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

How to edit a leaderstats value inside of a player (localplayer?)

Asked by 4 years ago

Hello!, i would just want to explain taht i am very new to scripting so sorry for any dumb mistakes. I am trying to give a player "coins" when they touch a chest and i have a coins value inside of a folder called leaderstats located in all players. When i try to give the player coins it gives me the error: 12:30:57.073 - Workspace.CHESTS.Part.Script:3: attempt to index nil with 'WaitForChild'

the script:

script.Parent.Touched:Connect(function()
    local player = game.Players.LocalPlayer
    player:WaitForChild("leaderstats").Coins.Value = player:WaitForChild("leaderstats").Coins.Value + 50
end)``

images: https://gyazo.com/63f44864750049f3dfd76aa2cabbd595

When playing the leaderstats looks like this https://gyazo.com/31ed8c7be918a2a2f6adacbed58ed63e

2 answers

Log in to vote
0
Answered by 4 years ago

Do not use the client. Exploiters can exploit the client to change the money that way. The leaderstats won't replicate to the server anyways cos you're editing it in the client. So instead, you should use a Server Script and place that Server Script's parent as the part.

script.Parent.Touched:Connect(function(hit) --We're gonna use the hit parameter.
    local player = game.Players:GetPlayerFromCharacter(hit.Parent) --This gets the player from our character.
    if player then --This is to make sure that the player isn't nil.
        player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 50 --Recommend you get a debounce (cooldown) as well.
    end
end)
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

So you're trying to assign local player to a script that isn't local, is it?

The alternative to this is: when the humanoid (part of the player's model) touches the chest, we can get his name.

Here's how to do it:

script.Parent.Touched:Connect(function(parttouched)
   if parttouched.Parent:FindFirstChild("Humanoid") then -- checking if the part that touched the chest is a member of the player's character in Workspace, becuase every character has a Humanoid.
    player = parttouched.Parent.Name -- Player name --
    game.Players[player]:WaitForChild("leaderstats")..Coins.Value =          
 game.Players[player]:WaitForChild("leaderstats").Coins.Value + 50 -- Using the [player] is finding an instance with the variable player name.
end
end)``
0
What the hell is game.Playersplayer[player]? Dovydas1118 1495 — 4y
0
Sorry, it's game.Players[player] bruno13bruno13 78 — 4y

Answer this question