So, I’m trying to make a kind of criminal game? Idk. But, im not that good at scripting and I need to know how to create a part that when you step on it, it adds more to a value of stuff on leaderstats. Example: You step on a part, and you have a Coins value inside leaderstats. When you step on the part, what happens is how much coins you have increases by 200. My current code is:
script.Parent.Touched:Connect(function(hit, player)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 200
end
end)
and the Leaderboard script:
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local coins = Instance.new("IntValue")
coins.Parent = leaderstats
coins.Name = "Coins"
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players[hit.Parent.Name]
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 200
end
end)
So what exactly isn’t working?
You left that out so it’s not really possible for me to help rectify that.
Your system might seem to work alright which is what you want, with the LocalScript with the code in the first section placed under the Part object in the hierarchy and the code in the second section made server-side placing it under ServerScriptService in a script object (also using parameters correctly for Part.Touched) - but it isn’t what you need, what you need is to increment any currencies server-side since you know the client can do whatever it wants and if you handle purchases client-side then it’s free to increment any currencies near infinitely for itself.
You can involve making purchases server-side using RemoteEvent or even RemoteFunction objects, this paradigm is commonplace in handling in-game currencies and their transactions, but then this all is not necessary in any way - just makes the process of making purchases more failsafe than generous to exploiters.
Note, no need to check for hit.Parent as a service would at least always be your parent and if not the part would be nil itself and it wouldn’t be able to touch the part as it’s not in workspace.
script.Parent.Touched:Connect(function(hit)
local debounce = true
if debounce then
debounce = false
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players[hit.Parent.Name]
player.leaderstats.Coins.Value += 200
end
debounce = true
end
end)
You should read about how debounces work in the article Vong25 linked, but here is a working one:
debounce = false
script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and debounce == false then
debounce = true
local player = game.Players[hit.Parent.Name]
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 200
wait(3)
debounce = false
end
end)