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
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.