I need to detect if a player hit a part and then get that player and award them a "Star" in my game.
So far I've tried the code below, which gives the star(s) to every player in the game.
script.Parent.Touched:connect(function() for _,Player in pairs(game.Players:GetPlayers()) do if Player:FindFirstChild("leaderstats") then Player.leaderstats.Stars.Value = Player.leaderstats.Stars.Value + 1 end end end)
I tried some other things but they didn't work, this is the closest I've gotten.
Any amount of help is appreciated very much.
script.Parent.Touched:connect(function(hit) --We get the player using the GetPlayerFromCharacter method which returns the player based on the character. If hit is not nil (or false) and hit.Parent is not nil (or false) we set the player variable to what that method would return local player = hit and hit.Parent and Game.Players:GetPlayerFromCharacter(hit.Parent); --If it was an actual player that touched the part if player then --We check if they have a leaderstats object, if so we assign stats variable to find the Stars object in leaderstats local stats = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Stars"); --If leaderstats.Stars exist if stats then --Increment it stats.Value = stats.Value + 1; end end end);