1
votes
local Player = game.Players.LocalPlayer
local PlayerCash = Player:WaitForChild("leaderstats"):WaitForChild("Strength")
local Mouse = Player:GetMouse()
local amount = 50

script.Parent.Activate:Connect(function()
game.Players.LocalPlayer.leaderstats.money.Value = game.Players.LocalPlayer.leaderstats.money.Value  + amount
end)

I am trying to make a code to give Strength when you click. When I press 'Play' it doesn't work. All it says in the output is 'attempt to index function with 'Connect'.'

2
Heyo, please don't post pictures of your code, please copy-paste the code here so that it is easier for us to talk about. - Kylaaa
There I changed it :) - Jackyboy98000

2 Answers

0
votes

The error "attempt to index [type] with [field name]" means that you are incorrectly using something like an object, and you are trying to access some field on it.

In your case, this error is pointing at the line : script.Parent.Activate:Connect.This says that script.Parent.Activate is a function and not a table, and you cannot call Connect on a function. This is just to help us figure out what is wrong with our code.

Since you working with Tools, there is a simple fix. Instead of using the Activate function, you are looking for the Activated event. So just change update that line like this :

script.Parent.Activated:Connect(function()
0
votes

It connects a function to an event that happened, like this:

local tool = script.Parent

 -- It connects the event (the tool being equipped) to a function.
tool.Equipped:Connect(function()
  -- CODE
end)

Also, the answer above is how you can fix your problem and here's a shortcut: You have defined "Player" and you could've used it inside the function. Have a great day and God bless everyone here.