Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Whats wrong with this onTouch?

script.Parent.Touched:connect(function()
	for _,Player in pairs(game.Players:GetPlayers()) do
	    if Player:FindFirstChild("leaderstats") then
	        Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value +1
	    end
	end
end)

What it will do is when you touch it, it will add 1 to the Player who touches its Wins. But it adds about 3

Also, I want to have it when you touch it to ALSO teleport the player to the lobby... problem being how? I know you have to CFrame their BodyRootPart but how would I find the player who touched it and CFrametheir BodyRootPart?

2 answers

✓ Accepted answer

The reason it was adding more than 1 point for touching it is because you had nothing stopping the script running again. This should work, but with your script it would have added points to every player in the game, so use this:

db = false -- Create a delay variable

script.Parent.Touched:connect(function(hit)
	if db == false then -- Checks if it's been recently used
		db = true -- Stops players from using it agian
		for i,v in pairs(game.Players:GetChildren()) do
			if v.Name == hit.Parent.Name then
				if v:FindFirstChild("leaderstats") then
					v.leaderstats.Wins.Value = v.leaderstats.Wins.Value + 1
					wait(10) -- Waits before it can be used again
					db = false -- Allows players to use it again
				end
			end
		end
	end
end)

2 comments

0

Updated it when I read about it adding more than 1 win. It now has a 10 second delay before it can be used again!

0

Thanks!

script.Parent.Touched:connect(function(h)
	if h.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:FindFirstChild(h.Parent.Name)
		if player then
			player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.