Hello! I am trying to make a script that gives you a point when you are between 100 and -5 on the y axis. There is no error in the output, but I still dont get any points. The only scoreboard objectives are Points and High Score. Here is the code I am using.
while wait(2.1) do if Workspace.Running.Value == true then for _,Player in pairs(game.Players:GetPlayers()) do for i,v in pairs(Workspace:GetChildren()) do if v.Name == Player.Character then player = v if Player:FindFirstChild("leaderstats") then leaderstats = Player.leaderstats points = leaderstats:FindFirstChild("Points") highscore = leaderstats:FindFirstChild("High Score") if player.Torso.Position.y < 100 and player.Torso.Position.y > -5 then points.Value = points.Value + 1 if points.Value > highscore.Value then highscore.Value = points.Value end end end end end end end end
Hopefully you can help!
Your problem is on line 5 of that code. Player.Character
is an Instance and v.Name
is a string, so that if statement will never be true.
As well, there's no reason to nest the loops like. It's more efficient to just access the Player's Character directly.
Here is your code, modified to work better:
while wait(2.1) do if Workspace.Running.Value == true then for _,Player in pairs(game.Players:GetPlayers()) do local player = Player.Character if player then leaderstats = Player:WaitForChild("leaderstats") points = leaderstats:WaitForChild("Points") highscore = leaderstats:WaitForChild("High Score") if player.Torso.Position.y < 100 and player.Torso.Position.y > -5 then points.Value = points.Value + 1 if points.Value > highscore.Value then highscore.Value = points.Value end end end end end end
Quick question, why do you work the ends into one line like that? It detracts from the code's readability.