Script is setting value instead of adding to it?

Hello, I’ve got an issue with a script. No idea whats causing the issue as everything seems fine and I’m not getting any weird output.

I have a script (below) which is supposed to give the player 1000 cash when they touch a part then it teleports them.

I’ve tried changing how the cash variable is declared, but it still sets the cash instead of adding 1000 which I’ve told the script to do.

Script:

local Pad = game.Workspace.Pad2

script.Parent.Touched:Connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) if player then
		local CurrentlyTeleporting = player.Character:FindFirstChild("CurrentlyT")
		local Cash = player.leaderstats.Cash
		if not CurrentlyTeleporting then return end
		
		if not CurrentlyTeleporting.Value then
			CurrentlyTeleporting.Value = true
			Cash.Value = Cash.Value + 1000
			player.Character.HumanoidRootPart.CFrame = Pad.CFrame + Vector3.new(0,5,0)
			wait(3)
			CurrentlyTeleporting.Value = false
		end
	end
end)

Sorry if the post is kind of rough, first time posting here.

You need to make sure the object has a humanoid. Try this -

local Pad = game.Workspace.Pad2

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        if player then
           local CurrentlyTeleporting = player.Character:FindFirstChild("CurrentlyT")
	       local Cash = player.leaderstats.Cash
		if not CurrentlyTeleporting then return end
		
		if CurrentlyTeleporting.Value == false  then
			CurrentlyTeleporting.Value = true
			Cash.Value = Cash.Value + 1000
			player.Character.HumanoidRootPart.CFrame = Pad.CFrame + Vector3.new(0,5,0)
			task.wait(3)
			CurrentlyTeleporting.Value = false
	   	    end
        end
    end
end)

Mind sharing relevant screenshots?

Try doing a += they should do the same thing, but setting isn’t one of them so maybe give it a shot

Thanks for the quick reply, I have the exact same thing happen with the script you supplied. It teleports the player, but sets the players value to 1000 instead of adding 1000.

https://youtu.be/Q-DuQ93sPFM Video Showing the described issue with my script

https://youtu.be/Ptf9Gb8Fcz4 Video Showing the same issue with your script

Ok so I feel like I know your issue, I think you are adding your other cash on the client and not the server, am I right?

2 Likes

Trying this now.

Just tried it same error is occurring.

https://youtu.be/4Hq-Eavbrhk Video showing the error occurring with your edit.

Thanks for the quick reply!

1 Like

Yup, the 10 being added every 5 seconds is controlled by a local script in starter player scripts. I can easily swap it to a server script, just made it a local script because it’s easier to get the player through one lol.

I’ll try swapping it to a server script here in a moment.

1 Like

Yeah well that is the issue, the server still thinks you have 0 cash (since you only added it on the client) so 0+1000=1000
Which makes it look like it’s setting it

1 Like

Just swapped to a server script and it’s Kind of working. It adds the 1000 but also a little extra, which I guess is better than resetting the players cash to 1000 lol.

Thanks for the help, and for the explanation on the issue appreciate it.

1 Like