Cooldown for spawn(function()

What I want to Achieve

Making a Cooldown for spawn(function()

The Script
local cleanuptime = 20 -- time when its gonna cleanup

function Raycast(origin, direction, ignorelist) -- we move raycasting to a function so the final function doesnt get long
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = ignorelist
	params.FilterType = Enum.RaycastFilterType.Blacklist

	local Result = workspace:Raycast(origin, direction, params)

	return Result
end

-- final function
function Splatter(c)
	local ray = Raycast(c.HumanoidRootPart.Position, Vector3.new(0,-1,0) * 10, {c})

	if ray then -- we check if there is a ray
		-- there is a ray :D!
		if ray.Instance then -- we check if hit something
			-- it hit something!
			for i = 1,math.random(2,4) do -- 1 splatter is kind of boring why not add more!
				local splatter = game.ServerStorage.BloodSplatter:Clone() -- clone the splatter from serverstorage
				splatter.Position = ray.Position + Vector3.new(math.random(1,3),0,math.random(1,3)) -- random position
				splatter.Orientation = Vector3.new(0,math.random(10,90),0) -- random orientation
				splatter.Parent = workspace -- now we parent it to the workspace)

				spawn(function() -- appear & cleanup

					-- tween service!
					local tween = game:GetService("TweenService"):Create(splatter.BloodTop, TweenInfo.new(1, Enum.EasingStyle.Sine), {Transparency = 0.7})
					tween:Play() -- makes the splatter appear
					wait(cleanuptime) -- waits the cleanup time
					local clean = game:GetService("TweenService"):Create(splatter.BloodTop, TweenInfo.new(5, Enum.EasingStyle.Sine), {Transparency = 1})
					clean:Play()

					clean.Completed:Connect(function()
						splatter:Destroy() -- destroy the splatter when cleaning is done!
					end)
				end)
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(P) -- when a player is added we...
	P.CharacterAdded:Connect(function(C) -- when a character is added we...
		C.Humanoid.HealthChanged:Connect(function()
			Splatter(C)
		end)
	end)
end)

I’m not really a fan of scripting so I do not know what I am doing, I’ve tried putting wait(10) below the spawn(function() but Nothing Seems to work. A little help would be appreciated.

Script is stored at ServerScriptStorage and the Model is stored at ServerStorage

Add a de bounce to it at spawn function thing

1 Like

I recommend using task.delay(time, function())
https://developer.roblox.com/en-us/api-reference/lua-docs/task

what are you trying to achieve vs what are you seeing happen?

if you want the splatters to happen one after another, wait 20 then clean up try this.
I use task.defer instead of spawn there is a slight difference that spawn will try to run immediately where defer will run later, since it immediately task.wait(cooldowntime) there is little difference.

function Splatter(c)
	local ray = Raycast(c.HumanoidRootPart.Position, Vector3.new(0,-1,0) * 10, {c})

	if ray then -- we check if there is a ray
		-- there is a ray :D!
		if ray.Instance then -- we check if hit something
			-- it hit something!
			for i = 1,math.random(2,4) do -- 1 splatter is kind of boring why not add more!
				local splatter = game.ServerStorage.BloodSplatter:Clone() -- clone the splatter from serverstorage
				splatter.Position = ray.Position + Vector3.new(math.random(1,3),0,math.random(1,3)) -- random position
				splatter.Orientation = Vector3.new(0,math.random(10,90),0) -- random orientation
				splatter.Parent = workspace -- now we parent it to the workspace)

				-- tween service!
				local tween = game:GetService("TweenService"):Create(splatter.BloodTop, TweenInfo.new(1, Enum.EasingStyle.Sine), {Transparency = 0.7})
				tween:Play() -- makes the splatter appear
				task.defer(function()
					task.wait(cleanuptime) -- waits the cleanup time
					local clean = game:GetService("TweenService"):Create(splatter.BloodTop, TweenInfo.new(5, Enum.EasingStyle.Sine), {Transparency = 1})
					clean:Play()
					clean.Completed:Wait()
					splatter:Destroy()
				end)
			end
		end
	end
end