How can i add a delay to this system?

Hello!

Currently, this code will immediately collect the coins when in radius. However, i would like it to wait 2 seconds before allowing you to collect them. I Tried adding a task.wait(2) however it just made you wait 2 seconds to collect every individual coin. I added this just below the if drop.magnitude line. please help! Thanks.

local player  = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local ts = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")

while true do
	for i, drop in pairs(workspace.Drops:GetChildren()) do
		if drop:IsA("Part") and drop:FindFirstChild("Value") then
			local magnitude = (HumanoidRootPart.Position - drop.Position).Magnitude
			if magnitude <= player.Data.MagnetReach.Value then
				local value = drop:WaitForChild("Value").Value
				local CurrencyName = drop:WaitForChild("CurrencyName").Value
				drop.CanCollide = false
				local tween = ts:Create(drop, TweenInfo.new(magnitude * 0.01, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {Position = HumanoidRootPart.Position})
				tween:Play()
				tween.Completed:Connect(function()
					rs:WaitForChild("GiveCurrency"):FireServer(CurrencyName, value)
					drop:Destroy()
					rs:WaitForChild("Sounds").Collect:Play()
				end)
			end
		end
	end
	
	task.wait()
end
3 Likes

Do you Have system Roblox like come error right about

2 Likes

No, i’m trying to add a delay to the code but everything i try won’t work lol

The code has no errors.

1 Like

Nice about to workhng script i know this is

2 Likes
local player  = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local ts = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")

while true do
	for i, drop in pairs(workspace.Drops:GetChildren()) do
		if drop:IsA("Part") and drop:FindFirstChild("Value") then
                        local hyeah = tick()
			local magnitude = (HumanoidRootPart.Position - drop.Position).Magnitude
			if magnitude <= player.Data.MagnetReach.Value and tick() - hyeah >= 2 then
				local value = drop:WaitForChild("Value").Value
				local CurrencyName = drop:WaitForChild("CurrencyName").Value
				drop.CanCollide = false
				local tween = ts:Create(drop, TweenInfo.new(magnitude * 0.01, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {Position = HumanoidRootPart.Position})
				tween:Play()
				tween.Completed:Connect(function()
					rs:WaitForChild("GiveCurrency"):FireServer(CurrencyName, value)
					drop:Destroy()
					rs:WaitForChild("Sounds").Collect:Play()
				end)
			end
		end
	end
	
	task.wait()
end

tick can work out

2 Likes

Thanks! but now it wont let me collect coins

1 Like
local player  = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local ts = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")

while true do
	for i, drop in pairs(workspace.Drops:GetChildren()) do
		if drop:IsA("Part") and drop:FindFirstChild("Value") then
                        local hyeah = nil
              if hyeah == nil then hyeah = tick() end
			local magnitude = (HumanoidRootPart.Position - drop.Position).Magnitude
			if magnitude <= player.Data.MagnetReach.Value and tick() - hyeah >= 2 then
				local value = drop:WaitForChild("Value").Value
				local CurrencyName = drop:WaitForChild("CurrencyName").Value
				drop.CanCollide = false
				local tween = ts:Create(drop, TweenInfo.new(magnitude * 0.01, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {Position = HumanoidRootPart.Position})
				tween:Play()
				tween.Completed:Connect(function()
					rs:WaitForChild("GiveCurrency"):FireServer(CurrencyName, value)
					drop:Destroy()
					rs:WaitForChild("Sounds").Collect:Play()
				end)
			end
		end
	end
	
	task.wait()
end

3 Likes

Do you understand what i’m trying to achieve? it seems as if you dont

1 Like

oh sorry i thought you mean like a cooldown before you can collect coins, let me rescript it rn

2 Likes

Oh so you do! I didn’t mean it in a rude way btw

1 Like

Im i need to be clearer: The coins should pop out, and after 2 seconds you should be able to collect them

1 Like
local player  = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local ts = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")

while true do
	for i, drop in pairs(workspace.Drops:GetChildren()) do
		if drop:IsA("Part") and drop:FindFirstChild("Value") then
                        local hyeah = nil 
			local magnitude = (HumanoidRootPart.Position - drop.Position).Magnitude
                        if hyeah == nil then hyeah = 0 end
			if magnitude <= player.Data.MagnetReach.Value then hyeah +=.01
                                if hyeah >=2 then
                                hyeah = -math.huge -- bad coding lol
				local value = drop:WaitForChild("Value").Value
				local CurrencyName = drop:WaitForChild("CurrencyName").Value
				drop.CanCollide = false
				local tween = ts:Create(drop, TweenInfo.new(magnitude * 0.01, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {Position = HumanoidRootPart.Position})
				tween:Play()
				tween.Completed:Connect(function()
					rs:WaitForChild("GiveCurrency"):FireServer(CurrencyName, value)
					drop:Destroy()
					rs:WaitForChild("Sounds").Collect:Play()
				end)
else hyeah = nil -- correct this to make this set to nil if out of range.
			end
		end
	end
	
	task.wait()
end
end

1 Like

The coins still wont collect.
Also i extremely lagged when joining

sorry if i dont understand what you’re trying to achieve im probably dumb

do you mean as if after 2 seconds (when the coin spawned) passed you’re able to collect them
or
as if you have to wait 2 seconds near them

1 Like

The first option lol. I probably wasn’t clear lol. sry

local player  = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local ts = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")

while true do
	for i, drop in pairs(workspace.Drops:GetChildren()) do
		if drop:IsA("Part") and drop:FindFirstChild("Value") and not drop:FindFirstChild('ThisIsACooldownThing') then
			local magnitude = (HumanoidRootPart.Position - drop.Position).Magnitude
			if magnitude <= player.Data.MagnetReach.Value then
				local value = drop:WaitForChild("Value").Value
				local CurrencyName = drop:WaitForChild("CurrencyName").Value
				drop.CanCollide = false
				local tween = ts:Create(drop, TweenInfo.new(magnitude * 0.01, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {Position = HumanoidRootPart.Position})
				tween:Play()
				tween.Completed:Connect(function()
					rs:WaitForChild("GiveCurrency"):FireServer(CurrencyName, value)
					drop:Destroy()
					rs:WaitForChild("Sounds").Collect:Play()
				end)
			end
		end
	end
	
	task.wait()
end

on the server you might wanna use debris to destroy the cooldown value

might be what you wanted?

2 Likes

Heya! Thanks for the help.

There is now no cooldown. But you can collect the coins.

If you need the server script, lemme know

you gotta add a boolvalue into the drop called ThisIsACooldownThing

and on serverscript use debris to destroy ThisIsACooldownThing in the drop

1 Like

I Have checked if the cooldown = false. Now what would i do on the server?

Alright, i cant do this. This is to difficult