I have made a script to make this orb only add 1 to your stats one time. It works if I do not tweak the specified value in orb folder, however if I attempt to change the value manually in test mode, it still awards an orb collected. The orbfolder has a datastore to it, but it doesnt seem to be the problem as I tested it in studio changing the value myself with the orb still being awarded. If anyone could identify what is wrong, I would appreciate it. My code is below:
game.Players.PlayerAdded:Connect(function(join) script.Parent.Touched:Connect(function(plr) local humanoidcheck = plr.Parent:FindFirstChild("Humanoid") if humanoidcheck ~= nil then local orbfolder = join.orbfolder if orbfolder.orb1.Value == 0 then local orbs = join.leaderstats.Orbs orbs.Value = orbs.Value + 1 orbfolder.orb1.Value = 1 end end end) end)
Thank you!
game.Players.PlayerAdded is unneeded here. At the moment your script will create a new touched event every time a Player joins, and hence will run the code every time it is touched for each Player.
What you want to do is to use :GetPlayerFromCharacter(char) to get the player instead:
script.Parent.Touched:Connect(function(part) local humanoid = part.Parent:FindFirstChild('Humanoid') if (humanoid) then local player = game.Players:GetPlayerFromCharacter(humanoid.Parent); if (player) then -- Run your code end end end)