Scripting problems

Hellooooo I need help with this simple script that isn’t working and I can’t find out what the error is. Here is the script

script.Parent.ClickDetector.MouseClick.CashGiver:Connect(function(player)
local PlayerCash = player.leaderstats.Cash
PlayerCash.Value = PlayerCash.Cash + 1

end)

1 Like

Alright, I think the issue is that you are stating PlayerCash.Cash instead do this:

PlayerCash.Value += 1
2 Likes

It didn’t work but I think you are right about PlayerCash.Value

PlayerCash.Cash, based on your definitions is player.leaderstats.Cash.Cash

That aside, I think the issue is whatever/wherever “CashGiver” is. I have doubts it is stored inside the event “MouseClick.” I’m guessing that is actually the parent?

I’d just drop “CashGiver” and see if that fixes it.

Try doing:

script.Parent.ClickDetector.MouseClick.CashGiver:Connect(function(player)
local PlayerCash = player.leaderstats.Cash
PlayerCash.Value = PlayerCash.Cash.Value + 1

end)

If it doesn’t work, then tell us the cash script. where the cash gets created.

It didn’t unfortunately :confused: but here is the cash script
local DataStorageService = game:GetService(“DataStoreService”)
local myDataStore = DataStorageService:GetDataStore(“myDataStore”)

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats

local playerUserId = "Player_"..player.UserId
--Load Data

local data
local succes, errormessage = pcall(function()
	data = myDataStore:GetAsync(playerUserId)
end)

if succes then
	Cash.Value = data
	--Setdata to the current cash
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”…player.UserId

local data = player.leaderstats.Cash.Value

local success, errormessage = pcall(function()
	myDataStore:SetAsync(playerUserId, data)
end)

if success then
	print("Data successfully saved!")
else
	print("error im gonna cry")
	warn(errormessage)
end

end)
game:BindToClose(function()
wait(5)
end)

I think the problem is you are putting .CashGiver instead of just .MouseClick and also the PlayerCash.Cash is PlayerCash.Value

1 Like

The last line should be
PlayerCash.Value = PlayerCash.Value + 1

I agree with the sentiment we need to see what errors are showing up in output.


Most of these might be plugins or other scripts in my game

I’m sure this will work

script.Parent.ClickDetector.MouseClick.CashGiver:Connect(function(player)
    
local PlayerCash = player.leaderstats.Cash
PlayerCash.Value = PlayerCash.Value + 1

end)


these 2 are from calling PlayerCash.Cash. Switch to PlayerCash.Value.

That might kill the script before other errors.

Whatever this CashGiver is, I think this is wrong. You should remove that.

Define the click detector and use += for the addition, like this:

local clickDetector = script.Parent:WaitForChild(“ClickDetectorNameHere”)

clickDetector.MouseClick:Connect(function(player)
local PlayerCash = player.leaderstats.Cash
PlayerCash.Value += 1
end)

If you want to add a cooldown then add this

local clickDetector = script.Parent:WaitForChild(“ClickDetectorNameHere”)
local cooldownActive = false
local cooldownTime = 1

clickDetector.MouseClick:Connect(function(player)
if cooldownActive == false then
cooldownActive = true
local PlayerCash = player.leaderstats.Cash
PlayerCash.Value += 1
wait(cooldownTime)
cooldownActive = false
end
end)

Thank you it worked!! I have one question though, why did you have an = after the plus?

I am a self taught scripter so I don’t really know why I just know how I can use it. My guess would be that = is the current value and when you add a plus it’s number plus the current value. Don’t quote me on it tho.

Can you check the post with the answer as the answer to this topic so anyone else who has the same problem can have a solution.

The += is shorthand for “increment” which then adds the other value to the current value. It was added as shorthand by programmers who got tired of typing the longer version.

1 Like
script.Parent.ClickDetector.MouseClick:Connect(function(player)
     local PlayerCash = player.leaderstats.Cash
     PlayerCash.Value += 1
end)

Also works with many other operators, the following operators are valid:

+=
-=
/=
*=
..=
%=
^=
2 Likes