How do I make it so a player's stone amount (IntValue) gets turned into money when they touch a part?

I’m trying to make it so whenever a player touches a part, their Stone amount gets turned into money, but I just can’t get it to work, what am I missing here? Here’s my code:

detector = script.Parent
debounce = false

detector.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid and not debounce then
		debounce = true
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local plrStats = player.Stats
		print(player)
		plrStats.Money.Value = plrStats.Money.Value + plrStats.Stone.Value
	end
end)

detector.TouchEnded:Connect(function()
	wait(3)
	debounce = false	
end)

You got any errors? Did it actually print(player)?

It did, I even added a print after the line where it changes the stats, it printed that aswell, but just doesn’t change the value.

Perhaps it is indeed changing the value, but plrStats.Stone.Value is 0 or something catchy is going on.
How is plrStats.Stone.Value being set? Is it in a local script.

The stone value is not 0, and the value is changed via a tool, and yes, it is in a local script. Is that a problem? I’m pretty much a beginner, so I probably don’t know about a silly mistake I’m making.

Changes to the client do not replicate to the server. The server will see a value of 0. Make sure these changes happen on a server script

3 Likes

That’s what’s going on then! Since you’re setting it from a localscript, the stone value is only being changed in the client, meaning in the server it remains 0. And since the stone is being added to money in server, and server seees stone as 0, nothing gets added. And don’t blame yourself for this mistake! You just gotta learn more, always remember this information about client (localscript) and server

2 Likes

Ohhhh, err, so do I change the local script into a normal script in the tool orr? Do I use remote events or something like that, as I said, I’m a beginner and don’t know much about this sort of stuff, but thanks!

Just put this script in a normal script not local script as it is executed on the server in a part.

If there is code that is required to be run on the client e.g. animations, you can use remote events, or just put the server stuff in a separate script. If there is no required client stuff, then yes, you can change to a server script.

1 Like

Hmh, there is some required client stuff, as I’m getting the local player and the player’s mouse, so I’ll have to use remote events, big thanks!

1 Like

No problem, hope this helped! If you have any other issues don’t forget to let us know

1 Like

For that specific script that you posted in this topic, you don’t need any local stuff.

The code that has the local stuff is in a tool that changes the stone value, that’s what his issue was. That script was just adding 0 because of it

Oh okay I didn’t notice that, thank’s for the clarification.

1 Like

Try this.

local detector = script.Parent
local debounce = false

detector.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if not debounce then
			debounce = true
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			local plrStats = player.Stats
			print(player)
			plrStats.Money.Value = plrStats.Money.Value + plrStats.Stone.Value
			plrStats.Stone.Value = 0
			wait(3)
			debounce = false
		end
	end
end)

Try using this:

detector = script.Parent
debounce = false

detector.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
		debounce = true
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local Stats = player:WaitForChild("Stats")
            if Stats ~= nil then
		    local Money = Stats:WaitForChild("Money")
                local Stone = Stats:WaitForChild("Stone")
                if Money and Stone ~= nil then
		            Money.Value = Money.Value + Stone.Value
                    Stone.Value = 0 -- if you don't add this the player will still have the stones, therefore the game will get exploited
                end
            end
        end
	end
end)

detector.TouchEnded:Connect(function()
	wait(3)
	debounce = false	
end)