Need to know how to create a part that when you step on it, it increases a value of your leaderstats

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)
1 Like
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)

Theres no ‘player’ paramiter

[/quote]

Ok thanks, ill test it out

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.

Instead of doing player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 200 you can just do player.leaderstats.Coins.Value += 200.

1 Like

I tried it - and it worked, but it goes too fast. Do I have to add a debounce loop?

Yes, when working with Touched, it is usually best to put a debounce as the touched event will fire a LOT in a very small amount of time.

Could you make an example for me?

Here is an article on the Developer Hub about debounce.

like this?

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)

@Cynacol Define the variable outside of the function. By the way, you can use Players:GetPlayerFromCharacter() instead of checking for a humanoid.

local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
    player.leaderstats.Coins.Value += 200
end

@Emper0rAwes0me I recommend making it a local variable. Also, instead of debounce == false you can just do not debounce.

Yeah my bad, I was typing it out in a rush. Either works though :smile:

1 Like

Thank you! This is going to be useful.

Don’t worry, I already did + thanks for the info.

1 Like