Repeating an fuction while it is playing

I dont know how to do this so I would like some insight on how to do it. Whenever the remote event is fired I want it to do the function below. I want it do repeat the function if it is fired again even if the function is still going. Is there any way how to do this?


local function east()
	local gyat = game.Players.LocalPlayer.folder.lastkilled
	local new = script.Parent.Frame.Killed:Clone() 
	local TweenService = game:GetService("TweenService")
	local TweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut) 
	local Position = {Position = UDim2.new(0.295, 0,0.371, 0)}
	local PlayThis = TweenService:Create(new, TweenInfo, Position)
	new.Visible = true
	new.Parent = script.Parent.Frame
	new.Position = UDim2.new(0.912, 0,0.371, 0)
	new.Text = "Killed "..gyat.Value
	PlayThis:Play()
	wait(3)
	new:Destroy()
end

game.ReplicatedStorage.Killed.OnClientEvent:Connect(function(MyPlayer)
	east()
end)

Your code should already achieve what you wanted. You can add a print statement into the function in order to confirm this.

1 Like

the function is not repeating because of the wait

1 Like

Have you verified that by putting a print statement into the function?

1 Like

yes i have it prints once


That shouldn’t be the case. If you add a print statement as the first line of the OnClientEvent thing, how many times does it print?

Im prininting on line 2 it prints only once

local function east()
	print("east") 
	local gyat = game.Players.LocalPlayer.folder.lastkilled
	local new = script.Parent.Frame.Killed:Clone() 
	local TweenService = game:GetService("TweenService")
	local TweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut) 
	local Position = {Position = UDim2.new(0.295, 0,0.371, 0)}
	local PlayThis = TweenService:Create(new, TweenInfo, Position)
	new.Visible = true
	new.Parent = script.Parent.Frame
	new.Position = UDim2.new(0.912, 0,0.371, 0)
	new.Text = "Killed "..gyat.Value
	PlayThis:Play()
	wait(3)
	new:Destroy()
end

game.ReplicatedStorage.Killed.OnClientEvent:Connect(function(MyPlayer)
	east()
end)

Can you put it on line 19 as well please

it prints 2 wice for line 19 but i want it to print twice on line 2

can you show the code used when it is on line 19?

local d = true

local function east()
	print("east")
	local gyat = game.Players.LocalPlayer.folder.lastkilled
	local new = script.Parent.Frame.Killed:Clone() 
	local TweenService = game:GetService("TweenService")
	local TweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut) 
	local Position = {Position = UDim2.new(0.295, 0,0.371, 0)}
	local PlayThis = TweenService:Create(new, TweenInfo, Position)
	new.Visible = true
	new.Parent = script.Parent.Frame
	new.Position = UDim2.new(0.912, 0,0.371, 0)
	new.Text = "Killed "..gyat.Value
	PlayThis:Play()
	wait(3)
	new:Destroy()
end

game.ReplicatedStorage.Killed.OnClientEvent:Connect(function(MyPlayer)
	print("weast")
	east()
end)

OnClientEvent should already be firing on a separate thread, therefore the “wait()” won’t be affecting the rest of the script.

even if it WASN’T, you could use task library and call with spawn or defer.

how would i do that?


spawn:

game.ReplicatedStorage.Killed.OnClientEvent:Connect(function(MyPlayer)
	print("weast")
	task.spawn(east)
end)

defer:

game.ReplicatedStorage.Killed.OnClientEvent:Connect(function(MyPlayer)
	print("weast")
	task.defer(east)
end)

It still not working
The item still in the frame is the template btw

I just realised its not firing the event,
Here is it the firing script:

local Bullet = script.Parent
local NearbyBulletSound = Bullet:WaitForChild('NearbyBulletSound')

local Damage = Bullet:WaitForChild("Damage")

local Damaged = false
Bullet.Touched:Connect(function(hit)
	
	local CharHum = hit.Parent:FindFirstChildOfClass("Humanoid")
	
	if hit and CharHum and CharHum.Health > 0 then
		
		game.SoundService.DingSound:Play()
		
		game.ReplicatedStorage.skbidi.OnServerEvent:Connect(function(MyPlayer)
			MyPlayer.folder.lastkilled.Value = hit.Parent.Name
			game.ReplicatedStorage.Killed:FireClient(MyPlayer) --fires hre
		end)
		
		
		CharHum.Health = CharHum.Health - Damage.Value
		NearbyBulletSound:Play()
		
		
	end
	wait(0.1)
	Bullet:Destroy()
end)

while wait(1.5) do
	Bullet:Destroy()
end

it would help quite a bit of i actually knew what was going on but its kinda hard to when your events are named “skibid” and variables named “gyat”

yea im sorry my brain is not it today, i think i know the problem, in the second script the skbidi event is for getting the local player but i soon realised that it is the problem and its kind of dumb, where i want to fire the event is when i deal damage to the enemy. But my scripts work how there damage id being delt by the physcial bullet not hit scan so the whole point of the skbiid event is to fire the local player when they click. But i dont really know how to get the lcoal player in a sever screipt with out a remote event or playeradded. I am new to coding so do you know another way?