How can i add a delay to this system?

Basically you want to do a respawn coins system?, I’m trying to figure what you want to do. I guess it because you do :Destroy() in the drop (Coin)

1 Like

No, im trying to add a 2 second wait before you can start collecting coins

Something like a 2 seconds cooldown after collecting one, or wait 2 seconds after collecting any coins?

1 Like

A 2 second cooldown before collecting any coins

But why the task.wait() didn’t work to you?, this could be a way to what you want to do. (A seconds cooldown before collecting any coins)

By the way, I just fixed my comment because was wrong.

1 Like

Yeah, task.wait() seems to be the way to do it. I can’t figure out how to do it tho :confused:

same logic still applies to your edited comment, i need a cooldown that waits 2 seconds before collecting any coins

Well, do this.

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

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 and deb == false then
               deb = true
               task.wait(2)
				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()
                    deb = false
				end)
			end
		end
	end
end

I just added a debounce and the task.wait(2) because If you just add task.wait() below the If statement magnitude checker, the task.wait will be infinite due de while loop and it will never completed.

2 Likes

Heya! Thanks, but it only collects 1 coin every 2 seconds. not al lof them

Okey, lest change the while loop then

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")



local function MagnitudeChecker(HumanoidRootPart, Drop)
	while true do
		local Magnitude = (HumanoidRootPart.Position - Drop.Position).Magnitude
		return Magnitude
	end
end



for i, drop in pairs(workspace.Drops:GetChildren()) do
		if drop:IsA("Part") and drop:FindFirstChild("Value") then
			local Magnitude  = MagnitudeChecker(HumanoidRootPart, drop)
			if Magnitude <= player.Data.MagnetReach.Value then
               task.wait(2)
				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
1 Like

This is annoying, they wont collect now.

I can send you the module script if you would like

local Drop = {}

function Drop.DropCurrency(originCFrame, object, amount, value)
	for i = 0, amount do
		local cloneObj = object:Clone()
		cloneObj.Name = "Drop "..i
		cloneObj.Value.Value = value
		cloneObj.CFrame = originCFrame
		cloneObj.Parent = workspace:WaitForChild("Drops")
		
		local randomX = {math.random(-10, -2), math.random(2,10)}
		local RandomZ = {math.random(-10, -2), math.random(2,10)}
		
		local velocity = Vector3.new(randomX[math.random(1,2)], math.random(10,100), RandomZ[math.random(1,2)])
		
		cloneObj.AssemblyLinearVelocity = velocity
		
	end
end
	
return Drop

Hello, I think I get what you’re trying to do. Judging from previous posts, if I’m understanding this right (correct me if I’m wrong), whenever a coin drops, you want there to be a two second delay before you can collect that specific coin/you don’t want freshly spawned coins to be immediately collected? If that’s the case, you’d have to be able to tell whether a coin was recently dropped. I think you should do something similar to what pumpkyrofl said:

you gotta add a boolvalue into the drop called ThisIsACooldownThing

and on serverscript use debris to destroy ThisIsACooldownThing in the drop

But instead of a boolean value, you could set a Cooldown attribute on the drop. When the drop is spawned, set it to true. After two seconds pass, set it to false. Then you could check in the coin collect script if the drop’s Cooldown attribute is false. If it is, that means it’s been there long enough and you can collect it. It would work something like this:

-- In the drop script
local Drop = {}

function Drop.DropCurrency(originCFrame, object, amount, value)
	for i = 0, amount do
		local cloneObj = object:Clone()
		cloneObj.Name = "Drop "..i
		cloneObj.Value.Value = value
		cloneObj.CFrame = originCFrame
                
        cloneObj:SetAttribute("Cooldown",true) -- Set to true when first spawned
        task.delay(2,function()
            cloneObj:SetAttribute("Cooldown",false) -- Set to false after 2 seconds
        end)

		cloneObj.Parent = workspace:WaitForChild("Drops")
		
		local randomX = {math.random(-10, -2), math.random(2,10)}
		local RandomZ = {math.random(-10, -2), math.random(2,10)}
		
		local velocity = Vector3.new(randomX[math.random(1,2)], math.random(10,100), RandomZ[math.random(1,2)])
		
		cloneObj.AssemblyLinearVelocity = velocity
		
	end
end
	
return Drop

The script above sets the Cooldown attribute to true when the drop is first spawned. Then, it spawns a delayed thread that will run in two seconds (or however long you want it to be) which will set it to false. Basically, Cooldown is true when it is first spawned, then set to false after two seconds pass.

-- In the coin collect script
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:GetAttribute("Cooldown") then -- Checks for Cooldown attribute/if the coin just spawned
			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

This is mostly the same except when it determines whether it can collect a coin, it now will also check that the Cooldown attribute is false, meaning it can be collected.

Tell me if there’s anything more I can do or if I got the wrong idea. Hope this helps!

1 Like

Tell me the value of player.Data.MagnetReach first of all.

1 Like

It just a stud range that you can collect things from

(12 studs)

Hello! After anticipating the last 20 minutes of your reply lol. I think you do and don’t have the right idea.

What i’m trying to achieve is after 2 seconds from when the coin is parented. All coins can be collected. not just 1

Try this again. This should be fixed

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")

local function MagnitudeChecker(HumanoidRootPart, drop)
	while true do
		local Magnitude = (HumanoidRootPart - drop).Magnitude
		return Magnitude
	end
end

for i, drop in pairs(workspace.Drops:GetChildren()) do
		if drop:IsA("Part") and drop:FindFirstChild("Value") then
			local Magnitude  = MagnitudeChecker(HumanoidRootPart.Position, drop.Position)
			if Magnitude <= player.Data.MagnetReach.Value then
               task.wait(2)
				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
1 Like

Nope, i still can’t collect coins. Thanks for your help as well if i haven’t said it already :smiley:

There’s any output error?, something like nil or … " is not a valid member"

1 Like

Lol, sorry I took so long (I try to make sure my posts make sense). I unfortunately still don’t get it… from what I can tell, what you’re saying is after one coin is spawned, you want all of them to be collectable? I wish I could help, I’m just not really sure what you mean… :neutral_face:

1 Like

Nope, not from what i can see. ill double check