How can I stop a script from instantly replaying?

I’m not exactly sure if the whole script replays, but I made one where you get 5 coins, a badge, and your character teleports to a location when you touch a part. Unfortunately, it seems to instantly replay, even though I tried adding a return function.

task.wait(0.75)

local BadgeId = 2363189416303957

local bs = game:GetService("BadgeService")

local telePart = game.Workspace.TelePartEasy

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		wait(0.2)
		local Player = game.Players[hit.Parent.Name]
		wait(0.2)
		player.Character.HumanoidRootPart.CFrame = telePart.CFrame + Vector3.new(0,5,0)
		wait(0.2)
		bs:AwardBadge(Player.UserId, BadgeId)
	player.leaderstats.Coins.Value = player.leaderstats.Coins.Value +5
	return
		print("Yay")

	end
end)

.Touched will continue to fire whilst you are physically interacting with the object in question; this includes standing still on top of the object. If you are intending to stop the script from working after the player has received the badge, include a check to determine if the player has the badge or not.

2 Likes

You need to add a debounce so it doesn’t trigger again.

Fixed code:

local bs = game:GetService("BadgeService")

local BadgeId = 2363189416303957

local telePart = workspace.TelePartEasy

local playerDebounces = {}

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if player and not playerDebounces[player] then
		playerDebounces[player] = true
		
		player.Character.HumanoidRootPart.CFrame = telePart.CFrame + Vector3.new(0,5,0)
		
		player.leaderstats.Coins.Value += 5
		bs:AwardBadge(player.UserId, BadgeId)
		
		print("Yay")
		
		task.wait(1)
		playerDebounces[player] = nil
	end
end)
4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.