Help with making a local script despawn a part

I’m trying to make a building feature but only on the client, I want to avoid people blocking spawn,
I’m also trying to make it wait a while (like 5 seconds) before it fades away and gets deleted

I tried making a Despawn using a “for I = 1, 10” but it stopped running at that point

keep in mind, I’m not a really good scripter

tool = script.Parent

tool.equipped:connect(function()
	wait(0.1)
	while true do
		wait()
		if tool.Parent:FindFirstChild("Part") == nil then
			--prevent the player blocking themselves by spamming the tool
			
			script.Enabled = false
			wait(0.3)
			script.Enabled = true
			
			--just basic part making stuff
			
			local Part = Instance.new("Part")
			Part.Parent = tool.Parent
			Part.Anchored = true
			Part.CanCollide = false
			Part.Size = Vector3.new(4, 0.5, 4)
			
			--make the highlight look like a highlight
			
			local highlight = Instance.new("Highlight")
			highlight.Parent = Part
			highlight.Adornee = Part
			highlight.DepthMode = ("Occluded")
			highlight.FillColor = Color3.new(0, 0, 255)
		end
		local Part = tool.Parent:FindFirstChild("Part")
		
		--Basicly this is positioning the Placement highlight
		local Root = tool.Parent:WaitForChild("HumanoidRootPart")
		local pos = Root.CFrame * CFrame.new(0, -1, -5)
		Part.CFrame = pos
	end
end)

tool.Unequipped:Connect(function()
	local Part = game.Players.LocalPlayer.Character:FindFirstChild("Part")
	Part:Destroy()
end)

tool.Activated:Connect(function()
	local Part = game.Players.LocalPlayer.Character:FindFirstChild("Part") --just checking if the player already has a Placement highlight
	if Part ~= nil then
		
		local high = Part:FindFirstChild("Highlight")
	
		high:Destroy()
		Part.Parent = game.Workspace
		Part.CanCollide = true
		
		--Despawn here
		
		
		Part:Destroy()
	end
end)
1 Like

Instead of turning the highlighted part into a “physical” part, you can instead make a copy of it and do what you want with it.

if Part ~= nil then
		
		local high = Part:FindFirstChild("Highlight")
		local placed_part = Part:Clone()
		placed_part.Parent = game.Workspace
		placed_part.Position = Part.Position
		placed_part.CanCollide = true
		placed_part:FindFirstChildWhichIsA("Highlight"):Destroy()

		--Despawn here


		Part:Destroy()
	end

Why didn’t I think of that, I might try it

1 Like

you used “part” instead of “placed_part” but its all good

1 Like

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