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)
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.
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
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!
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.
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)
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)