Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I need help, Add the leaderboard when a player touches a coin?

I've been going around the internet how to add to a leaderboard! What I want to do is: when a player touches a coin, add to the "Coins" leaderboard, I've searched around the web, but all answers seem to be different!

I have the scripting for the coins done, and "Coins" is on the leaderboard, but I just don't know how to get it to go up! help!

Is there even a way to do so?
If not, should I just give up?

what I have is:

local cash = Instance.new("IntValue")
cash.Name = "Coins"
cash.Value = 0
cash.Parent = stats

And that works alright, I think.

The coins were not made by me. but, they get destroyed after you touch them.

What I've gathered together from the internet is:

workspace.Coin.CoinBoundingBox.Touched:connect(function(hit)
    cash.Value = cash.Value + 1
end)

And that doesn't work.

Any assistance would be greatly appreciated.

2 answers

This is incorrect because if you do it server-side you need GetPlayerFromCharacter() to fetch the player. Example:

script.Parent.Touched:Connect(function()
	local plr = game.Players:GetPlayerFromCharacter(script.Parent)
	if plr then
		if plr:FindFirstChild("leaderstats", true) then
			local ls = plr:FindFirstChild("leaderstats", true)
			ls.cash.Value = ls.cash.Value + 1
		end
	end
end)

After a player touches this part they will have 1 more cash point.

6 comments

0

why in the world are you looking in the Backpack for the leaderstats folder?

0

leaderstats is normally placed in the backpack

0

fixed it

0

leaderstats is normally placed in the player

0

what is it touching? If the Script's parent happens to be the player's character, that will run the script every time an animation plays or the player steps on something. Even then... WHAT ARE YOU DETECTING THE TOUCHES FROM?!?!?! THE PLAYER'S CHARACTER IS A MODEL!!!

0

Relatively, the character is a property of the player and IS NOT a child, it is also a model. Also, you're completely wrong.

This is a simple fix:

Now, I'm going to combine a leaderstats check in this, so remove that if needed.

					--[[
Single-Player API:
local plr = game.Players.LocalPlayer
local leaderstats = plr.leaderstats

-- Use a LocalScript (Client-Side)
					--]]


script.Parent.Touched:Connect(function(part)
	--[[
	Multiplayer API
	if game.Players:GetPlayerFromCharacter(part.Parent) then
		local plr = game.Players:GetPlayerFromCharacter(part.Parent)
	end

	-- use a plain Script (Server-Side)
	--]]

	if not leaderstats then
		leaderstats = Instance.new("Folder")
		leaderstats.Parent = plr
		leaderstats.Name = "leaderstats"
	end

	if leaderstats.Coins then
		local Coins = leaderstats.Coins
	else
		local Coins = Instance.new("IntValue")
		Coins.Name = "Coins"
		Coins.Parent = leaderstats
	end

	Coins.Value = Coins.Value + 1 -- or any amount you want really, especially if you have a system with pennies, dimes, and quarters etc.
end)

So one thing that I noticed you didn't include was most o your script. I only typed the portions that you requested, however I might not be completely sure that the script works with everything else you have smoothly, so tweak as needed.

2 comments

0

why are you using the second parameter it causes lag

0

Did not know that... I will fix that.

This discussion is preserved as it was. New answers, comments, and votes are closed.