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

I am trying to make a leader board but its not working?

I have an IntValue called leaderstats in the player and another IntValue called coins in leaderstats. I have a part that when touched I want it to add 10 to the Coins. Here is my script

local giver = script.Parent
local player = game.Players.LocalPlayer

local function SteppedOn(part)
	player.leaderstats.Coins.Value + 10
	
		
	
end

giver.Touched:connect(SteppedOn)

I can't figure out what I did wrong.

3 answers

✓ Accepted answer

I assume you are using a Server Script. LocalPlayer is for LOCAL SCRIPTS ONLY!!! So, if you want to find out who the player is, then you need to do it in another way. Think of it: If someone else that is not you steps on the giver, then you'll still earn the points! Also, the character in Workspace is a model, and the Touched event only returns the part that touched (triggered) it. So, you need to refer to its parent in order to get the player.

Apart from that, in line 5, you are trying to change the leaderstats the wrong way. Let me help you fix it.

Here is the script:

local giver = script.Parent
local db = false

local function SteppedOn(part)
    if not db then -- UPDATE: Added debounce! Very important for events like touched so it doesn't run multiple times!
        db = true
        local player = game.Players:GetPlayerFromCharacter(part.Parent) -- Get the player by the part that hit the giver.
        if player then -- Check to see if a player is found.
            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10 -- Change the value by adding 10 to it.
        end
        wait(1)
        db = false
    end
end

giver.Touched:connect(SteppedOn)

Any questions? Leave them in the comments below! Thanks! :)

3 comments

0

Just a suggestion, adding a Debounce, otherwise the function will be run multiple times on touch.

0

updated!

0

Thanks so much! I will look into learning more about debounce.

Your problem is simple. You're trying to add to a value, but you're not actually setting the value.

Here's the fixed code:

local giver = script.Parent
local player = game.Players.LocalPlayer

local function SteppedOn(part)
    player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10



end

giver.Touched:connect(SteppedOn)

1 comment

0

Also, as excellentAnarchy said, the LocalPlayer value cannot be accessed through a server script.

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