My script does not change the position after 5 seconds

local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local humroot = char:WaitForChild("HumanoidRootPart")

local cash = game.ReplicatedStorage.BossItem.Handle

hum.Died:Connect(function()
	local cashClone = cash:Clone()
	

	cashClone.Parent = game.Workspace
	
	cashClone.CFrame = humroot.CFrame
	wait(5)
	cashClone.CFrame = game.Workspace.Part.CFrame
	
end)
1 Like

Could you provide this information:

  • Are there any errors?
  • Does anything print?
  • A brief explanation on what you’re trying to make.

And going by what the title is:

  • Does the part clone and parent to the workspace?
  • Does it do the same thing for everytime the player dies?

This script is made to spawn a gem in the game where the npc died it spawns perfectly but it does not destroy itself after 5 seconds and it is parented to the workspace

Right- it is parenting to the workspace after 5 seconds because that’s what you told it to do? Am I misunderstanding?

It is parenting to the workspace so it can be collected i do not know how to destroy it after 5 seconds so i just try to put it another location far from the players but it does not do that

You can use :Destroy() or game.Debris:AddItem()?

hmm it seems to not be working in the gem there is a script which is click detector which allows you to click the gem and for you to be rewarded i could try to detect when the player has clicked it and then destroy it somehow but i dont know the first clue how to do that heres the other script

local money = script.Parent.Parent
local clickDetector = money.ClickDetector

clickDetector.MouseClick:Connect(function(plr)

local cash = plr.leaderstats.Coins
local exp = plr.Data.Exp
cash.Value += 40000
exp.Value += 500
money:Destroy()

end)

You know how to use :Destroy()?

yep

wait(5)
cashclone:Destroy()

Then can you remind me of what’s wrong? I’m very confused, especially on what you know and don’t know.

I want the part to change positions or be destroyed after 5 seconds

So you want the gem’s position to be animated, and also be destroyed after 5 seconds?

yes that is what i want to happen

Okay, I do believe that this could have been explained in the original post.
This is also late because I had to go somewhere.

A few things you should have so this script works:

  • ‘Gem’ object in ReplicatedStorage
  • The gem will have a ClickDetector
  • A folder called ‘Gems’ in the workspace
  • A folder called 'NPC’s in the workspace
--\\ Server Side:

local function SpawnGems(at, amount)
    local gem = game.ReplicatedStorage.Gem
    gem:PivotTo(CFrame.new(at))
    gem.Parent = workspace.Gems
    gem.ClickDetector.MouseClick:Connect(function(player)
        gem:Destroy()
        player.leaderstats.Coins.Value += amount
        player.Data.Exp.Value += 500
    end)
    game.Debris:AddItem(gem, 10)
end

local function OnNPC(npc)
    local hum = npc:FindFirstChildOfClass("Humanoid")
    if hum then
        hum.Died:Connect(function()
            SpawnGems(hum.RootPart.Position, 40000)
        end)
    end
end

workspace.NPCs.ChildAdded:Connect(OnNPC)
--\\ Client Side:

workspace.Gems.ChildAdded:Connect(function(gem)
    gem:SetAttribute("InitPos", gem.Position)
    gem:SetAttribute("Creation", tick())
end)

game:GetService("RunService").RenderStepped:Connect(function(dt)
    for _, gem in pairs(workspace.Gems:GetChildren()) do
        if gem:GetAttribute("InitPos") then
            local c = CFrame.new(gem:GetAttribute("InitPos") + Vector3.new(0,math.cos(tick()*2),0))
            local rotate = CFrame.Angles(0,math.rad(tick()*90)),0)
            gem.CFrame = c * rotate
            local lifetime = math.clamp((tick() - (gem:GetAttribute("Creation") or tick())) * 2, 0, 1)
            gem.Transparency = 1 - lifetime -- Fade In Gem (hopefully math is good)
        end
    end
end)
1 Like

I think your error might be that because the script is parented to the character it is basically being disabled as soon as the character is :Destroy()'d.
To test this:
I ran this code:


In here:
image

This was my output after resetting my character:


As soon as the character respawned and the old one was deleted, the for loop stopped.

yes the script is parented to the humanoid