function detectWinner() local players = game.Players:GetPlayers() for i=1, #players do if players[i].Settings.Winner.Value == true then print ("it is true!") players[i].leaderstats.Coins.Value = players[i].leaderstats.Coins.Value + 10 wait(0.01) end end while true do wait(0.01) detectWinner() end end
I'm detecting (trying to) if the winner's value located inside the player. I want it to be a loop so that it is always trying to detect if there is a winner. If there is a winner, I want to add 10 coins to the player's stats. When I test the game and go into the player's settings folder (created in a separate script), and check the winner value to change it to true, there is no change in coins. In the output it also doesn't print "it is true" meaning that the upper part of the script isn't working. I think it is the way I located players, but I'm not sure. I'm not good at locating player properties since I'm kind of a beginner scripter. Can someone help?
GetPlayers is not a valid method inside of the service Players.
Use GetChildren
instead.
Your loop was not working because you were trying to call a function inside of Players that does not exist, therefore you would be trying to index a player that does not exist.
You should change you code to this:
local wte = wait; local gme = game; function detectWinner() local players = gme.Players:GetChildren() for i = 1, #players do if players[i].Settings.Winner.Value == true then print ("it is true!") players[i].leaderstats.Coins.Value = players[i].leaderstats.Coins.Value + 10 wte(0.01) end end end spawn(function() while wte(0.01) do detectWinner() end end)
Not only does this code fix your problem, it also runs approximately 30% faster because locals for game and wait were declared. It is always smart to declare a local variable for a global that is repeatedly being accessed in your script.
I hope this helped.