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

I was wondering how to make this?

Asked by 10 years ago

I am making a script so the first person who touches the button each round gets a point. Whenever i try it a red line goes under and breaks the script. Here is the script

if game.Players:GetPlayerFromCharacter(hit.Parent) then game.Players:GetPlayerFromCharacter.Leaderstats.Points.Value = game.Players:GetPlayerFromCharacter.Leaderstats.Points.Value + 1 end
0
Is the Leaderstats and points value in the character? Or just in the Player. Try moving the values in Player so it wont get messed up online. Orlando777 315 — 10y
0
How? haydebug2003 -5 — 10y

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

I assume this is in a touched event?

Anyways, on lines 2 and 3 you are using GetPlayerFromCharacter(), but failing to provide the character argument. You are basically trying to get the player from the character of nothingness. Try this:

if game.Players:GetPlayerFromCharacter(hit.Parent) then
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    plr:WaitForChild("Leaderstats"):WaitForChild("Points").Value = plr.Leaderstats.Points.Value + 1
end

I also used WaitForChild() in case Leaderstats is not fully loaded when the code runs.

Note that the leaderstats will probably not show up on the in-game leaderboard because you named it 'Leaderstats' instead of 'leaderstats'. Also note that since PlayerAdded events don't fire in PlaySolo mode, there will likely not be any leaderstats if you're trying to test it in PlaySolo.

Because of the nature of Touched event, you may want to look into adding a debounce.

Ad

Answer this question