Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

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

0
Just a suggestion, adding a Debounce, otherwise the function will be run multiple times on touch. excellentAnarchy 50 — 7y
0
updated! starlebVerse 685 — 7y
0
Thanks so much! I will look into learning more about debounce. User#15461 0 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

If I am right, LocalPlayer cannot be accessed through a Server Script. To access the player find the Player through the Character touching the Part.

Also a Debounce might be useful to you, otherwise touching the Part will spam the function multiple times.

GetPlayerFromCharacter: http://wiki.roblox.com/index.php?title=API:Class/Players/GetPlayerFromCharacter

Debounce: http://wiki.roblox.com/?title=Debounce

Log in to vote
0
Answered by 7 years ago

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)

0
Also, as excellentAnarchy said, the LocalPlayer value cannot be accessed through a server script. antonio6643 426 — 7y

Answer this question