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

script that repeats itself without stopping ?

hello

I have a very annoying little problem, actually I have a death animation (not finished) but actually if the brick that kills the player stays in contact with a member of the player, the script repeats itself without stopping, it's very annoying

server-side script

local Brick = script.Parent


local repstorage = game:GetService("ReplicatedStorage")

local Remote = repstorage:WaitForChild("RemoteKill")
--End--

--Code--
local function PlayerTouch(Part)

	local Parent = Parent Share
	if game.Players:GetPlayerFromCharacter(Parent) then
		Parent.Humanoid.Health = 0
		for i,v in pairs(game.Players:GetChildren()) do
		Remote:FireClient(v, v.Name)
end
	end
end

Brick.Touched:connect(PlayerTouched)


local script

-----------------------------------------------------------------------------------------------------------------


local repstorage = game:GetService("ReplicatedStorage")
local remoteKill = repstorage:WaitForChild("RemoteKill")
local sound = game:GetService("Players")


remoteKill.OnClientEvent:connect(function(playerName) --[[playerName is (v.Name) that you fired from server]]--
	sound = game.Players[playerName].PlayerGui.Sounds
	

sound.Chase:Stop()
sound.Killed:Play()
print(playerName..." has been killed by Sonic.EXE")


end)

Is there any way to make the OnTouch disabled?

1 comment

0

Use a debounce

1 answer

✓ Accepted answer

There are two ways of limiting the connected function of the event, either you use a debounce that limits when the function can be called again, or you simply disconnect the connection as soon as it's used once.

Debounce

local debounce = false
Event:Connect(function()
	if not debounce then
		debounce = true -- blocks code from running again
		wait(5)
		debounce = false -- unblock and allows a new run
	end
end)

Disconnect

local connection = Event:Connect(function()
	connection:Disconnect() -- removes the connection after use
end)

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