Here is my terrible MS Paint diagram. Hope it helps
Ok, first off Iām sorry for not understanding. From the āterribleā diagram, it looks like you want the coins to drop and then be collected automatically after two secondsā¦? Is that rightā¦? Or do you want them all to be collected at once? If itās the latter (collect all at once) then I have an idea, but please tell me that thatās actually what you want first before I go and write up the wrong solution .
Lol! Okay, heres a step by step example:
- The coins have spawned into and have been parented to the folder
- Wait 2 seconds
- You can collect the coins if you are in range of the magnitude (12 studs) or in the script. The magnitude is defined as MagnetReach
add a value called cooldown, put a task.wait(2) and make it true and make the code an if statement.
All of us have tried this. But it collects 1 coin every 2 seconds not all of them
If you need me to be clearer. The coins rest on the floor before 2 seconds before all of them can be collected if there in range
I think I get it now- several coins are being dropped at once, and they should be collected together. Didnāt want to leave you in suspense, writing up a solution now!!!
YESS! WE GOT IT!!!
Yes, several coins drop at once. But they instantly get collected in range. I want a 2 second delay before they get collected
And they can only get collected in the range (if Magnitude <= player.Data.MagnetReach.Value then)
Hooray! Then hereās my solution, I sincerely hope this is what you need (sorry for the wait again ):
Drop Script
local Drop = {}
Drop.CooldownTime = 2 -- Specify how long the cooldown is in seconds
function Drop.DropCurrency(originCFrame, object, amount, value)
local drop = Instance.new("Model") -- The coins are put into their own Model in the Drops folder to show that they go together
drop:PivotTo(originCFrame)
drop.Name = "DropModel"
drop.Parent = workspace:WaitForChild("Drops")
for i = 0, amount do
local cloneObj = object:Clone()
cloneObj.Name = "Drop "..i
cloneObj.Value.Value = value
cloneObj.CFrame = originCFrame
cloneObj.Parent = drop
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
drop:SetAttribute("Cooldown",true) -- Cooldown applied to the group of coins rather than each specifically
task.delay(Drop.CooldownTime,function()
drop:SetAttribute("Cooldown",false)
end)
for i = 0, amount do
local cloneObj = object:Clone()
cloneObj.Name = "Drop "..i
cloneObj.Value.Value = value
cloneObj.CFrame = originCFrame
cloneObj.Parent = drop
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
drop:SetAttribute("Cooldown",true) -- Cooldown applied to the group of coins rather than each specifically
task.delay(Drop.CooldownTime,function()
drop:SetAttribute("Cooldown",false)
end)
drop.Parent = workspace:WaitForChild("Drops")
end
return Drop
Coin Collect:
local debris = game:GetService("Debris")
local player = game:GetService("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 j, dropModel in pairs(workspace.Drops:GetChildren()) do
if not dropModel:IsA("PVInstance") or dropModel:GetAttribute("Cooldown") then continue end -- If it's not a Model or Part, skip!
local canCollect = false
for i,drop in pairs(dropModel:GetChildren()) do
local magnitude = (HumanoidRootPart.Position - dropModel:GetPivot().Position).Magnitude
if magnitude <= player.Data.MagnetReach.Value then
canCollect = true
break -- Can collect, break loop to prevent unneccessary checking
end
end
if canCollect then -- If the player can collect the coins,
local c = 0 -- Count of coins being collected. You'll see why below
for i, drop in pairs(dropModel:GetChildren()) do -- Collect each!
if drop:IsA("Part") and drop:FindFirstChild("Value") then
local magnitude = (HumanoidRootPart.Position - drop.Position).Magnitude -- The magnitude relative to the coin
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()
c += 1
tween.Completed:Connect(function()
c -= 1
if c < 1 then -- Only once there are no more coins being animated, do we destroy the model. This is to make sure each tween completes properly and the player isn't scammed out of their coins!
dropModel:Destroy()
end
rs:WaitForChild("GiveCurrency"):FireServer(CurrencyName, value)
drop:Destroy()
rs:WaitForChild("Sounds").Collect:Play()
end)
end
end
end
end
task.wait()
end
Donāt know if it works, havenāt tested, please reply any errors, AAA
Ill test it!
Also, do i need to add a attribute to the part?
To each coin individually, no. I parented the coins to models, so I just put the attribute on the model and got it from the model. Also just found a problem in the code, about to edit
If you mean whether you have to use attributes, no, but I find it easier
So if i just replace the scripts, it should work?
I think soā¦
I hope soā¦
Weāll see, but yes it should work!
No, it doesnt work. my pc blew up so im messaging you on tablet.
JOKING" IT WORKS! THANK YOU SO MUCH!
Although the coins dont sprout out and spread out as much anymore.
YIPPEEEEEEEEEEEEEEE
When you say they donāt spread out as much anymore, are they disappearing faster than they spread? If thatās the case, just change the cooldown in the drop script to make them last longer. Iām glad I could help! (First ever solution btw :D)
No, before they typically sprouted and spreaded out further, now it seems to be in a line.
When they spawned in, there were in a line
Oh noes! I donāt think I changed that part of the script at all! Iāll see what I can doā¦
Oof, I donāt knowā¦ I canāt see what the issue could beā¦ :o
Lol, i removed the solution just in case. Ill have a look myself
They use to shoot out the head and fall back down. seems like they just sit and fall of the head now
Alright, made some changes to the solution, maybe try it now? Not sure if itās fixed or not but worth it to test it
OHHH I think I know what it could be. Iāll try something