How to set up a timed event using a proximity prompt

I am currently trying to create a timed event for example if you don’t interact with this within 10 seconds then this happens or if you interact with it within the 10 seconds then another thing happens

both server sided scripts

local rep = game:GetService("ReplicatedStorage")
local StaggerEvent = rep.FInisherEvents:WaitForChild("Stagger")
local serverstorage = game:GetService("ServerStorage")
local Assests = serverstorage["Finishing move stuff"]
local SeeEvent = rep.FInisherEvents:WaitForChild("No see")
local starting = rep.FInisherEvents:WaitForChild("StartFinish")
local kill = rep.FInisherEvents:WaitForChild("Kill")

local canfinish = true

StaggerEvent.OnServerEvent:Connect(function(player)
	if canfinish == true then
		canfinish = false
	print(player)
	local Prompt = Assests:FindFirstChild("Finisher")
	Prompt.Enabled = false
	local prompt2 = Prompt:Clone()
	prompt2.Enabled = false
	
	prompt2.Parent = player.Character:FindFirstChild("Torso")
	prompt2.Enabled = true
	local root = player.Character:FindFirstChild("HumanoidRootPart")
	root.Anchored = true
		SeeEvent:FireClient(player)
	elseif wait(10) then
		canfinish = false
	end
end)


kill.OnServerEvent:Connect(function(player)
	print("death")
	local humanoid = player.Character:FindFirstChild("Humanoid")
	humanoid.Health = 0
end)
local prompt = script.Parent
local playerr = prompt.Parent
local starting = game.ReplicatedStorage.FInisherEvents:FindFirstChild("StartFinish")
local serverstorage = game:GetService("ServerStorage")
local Assests = serverstorage["Finishing move stuff"]
local Begin = game:GetService("ReplicatedStorage").FInisherEvents:WaitForChild("Begin")

local function Finish ()

	local Prompt = Assests:FindFirstChild("Finisher")
	Prompt.Enabled = false
	
	local char = prompt.Parent.Parent

	char.HumanoidRootPart.Anchored = true
	
	local man = game.Players:GetPlayerFromCharacter(char)
	Begin:FireClient(man)

end

prompt.Triggered:Connect(function(player)

	wait(0.7)
	print(player)
	local prompt = playerr:FindFirstChild("Finisher")
	prompt.Enabled = false
	Begin:FireClient(player)
	Finish()
	
end)

I don’t think I’ve written it correctly I have multiple event shooting to multiple scripts.

Set a variable as os.clock() You could task.spawn or task.delay a function which waits ten seconds and if a value is or isn’t true then do one thing and disable the proximity prompt. And triggering the proximity prompt will find the difference of the current os.clock() time to the os.clock() time you had before and after doing checks it will cancel the previous thread.

local timeBegan 
local proximityPrompt = -- path to proximity prompt

StaggerEvent.OnServerEvent:Connect(function(player)
-- Do the checks you were doing previosly here. 
timeBegan = os.clock()

local thread = task.delay(10, function()
     -- check the value you changed when the execution happens and use else if statements to determine if it's happened or not
     -- Add the functionality here(what happens if the prompt isn't pressed)
end)

proximityPrompt.Triggered:Connect(function()
     if os.clock() - timeBegan <= 10 then
      -- Add what happens when the prompt is triggered in time. 
     task.cancel(thread)
      -- If you cancel the thread here you don't really need to do any checks inside the thread that was 
      --task.defered, 
     --otherwise you can let the thread run without cancelling and the checks there will stop it. 
     end
end)
2 Likes

Thank you this helped I didn’t even know you could set up a thread like this

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