Currently i made a new script for my cookie clicker game and i don't understand, when im clicking on the cookie with the script all the players in the server gets a cookie? How do i make it a certain player who clicks it gets the cookie?
debounce = false function onClick(Player) if not debounce then debounce = true for _,Player in pairs(game.Players:GetPlayers()) do if Player:findFirstChild("leaderstats") then Player.leaderstats.Cookies.Value = Player.leaderstats.Cookies.Value +1 wait(0.1) debounce = false end end end end script.Parent.ClickDetector.MouseClick:connect(onClick)
Your problem is that for _,Player
means for every player
. So what you are essentially doing is saying that for every player in the game, you want to increase their Cookies value by 1
Here is the solution it should work:
debounce = false script.Parent.ClickDetector.MouseClick:connect(function(Player) if not debounce then debounce = true local playerName = game.Players:WaitForChild(Player.Name) if playerName:findFirstChild("leaderstats") then playerName.leaderstats.Cookies.Value = playerName.leaderstats.Cookies.Value +1 wait(0.1) debounce = false end end end)
If it doesn't work, please comment and I will fix whatever is wrong.