I’m working on a game where I need a tool to respawn in the Workspace under certain conditions. Specifically, I want it to respawn if:
The player holding the tool leaves the game
The player holding the tool dies
The tool is otherwise removed from the in some way
This was the code i tried but there were issues like the tool spawning more than 1 when the tool wielder didn’t even die:
local originalScythe = workspace:FindFirstChild("Scythe")
if not originalScythe then
error("Scythe not found in Workspace! Please ensure it is placed there before starting.")
end
local originalPosition = originalScythe.Handle.Position
local originalOrientation = originalScythe.Handle.Orientation
local function respawnScythe()
if workspace:FindFirstChild("Scythe") then return end
local scytheClone = originalScythe:Clone()
scytheClone.Parent = workspace
scytheClone.Handle.Position = originalPosition
scytheClone.Handle.Orientation = originalOrientation
end
workspace.ChildRemoved:Connect(function(removedChild)
if removedChild.Name == "Scythe" then
wait(3)
respawnScythe()
end
end)
game.Players.PlayerRemoving:Connect(function(player)
if player.Character and player.Character:FindFirstChild("Scythe") then
wait(3)
respawnScythe()
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character.Humanoid.Died:Connect(function()
if character:FindFirstChild("Scythe") then
wait(3)
respawnScythe()
end
end)
end)
end)
First, I recommend using task.wait() instead of just wait() because it is much faster.
Second, the reason why it’s still respawning before the player dies is because you have a function that listens when something is removed from the workspace and im assuming your taking the weapon and putting it into the players backpack. This just adds back the sword right away even thought the player is still alive.
Third, if the tool is placed in the players backpack it only appears in the players character if it’s equipt, if they die without it equipt, you won’t find anything.
local originalScythe = workspace:FindFirstChild("Scythe")
if not originalScythe then
error("Scythe not found in Workspace! Please ensure it is placed there before starting.")
end
local originalPosition = originalScythe.Handle.Position
local originalOrientation = originalScythe.Handle.Orientation
local function respawnScythe()
if workspace:FindFirstChild("Scythe") then
return
end
local scytheClone = originalScythe:Clone()
scytheClone.Parent = workspace
scytheClone.Handle.Position = originalPosition
scytheClone.Handle.Orientation = originalOrientation
end
game.Players.PlayerRemoving:Connect(function(player)
if player.Backpack:FindFirstChild("Scythe") or player.Character:FindFirstChild("Scythe")) then
task.wait(3)
respawnScythe()
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character.Humanoid.Died:Connect(function()
if character:FindFirstChild("Scythe") or player.Backpack:FindFirstChild("Scythe") then
task.wait(3)
respawnScythe()
end
end)
end)
end)
-- Ensure the original scythe exists in the Workspace
local originalScythe = workspace:FindFirstChild("Scythe")
if not originalScythe then
error("Scythe not found in Workspace! Please ensure it is placed there before starting.")
end
-- Store the original position and orientation of the Scythe
local originalPosition = originalScythe.Handle.Position
local originalOrientation = originalScythe.Handle.Orientation
-- Function to respawn the Scythe
local function respawnScythe()
-- Prevent duplicate Scythe respawns
if workspace:FindFirstChild("Scythe") then return end
-- Clone the Scythe and set its properties
local scytheClone = originalScythe:Clone()
scytheClone.Parent = workspace
scytheClone.Handle.Position = originalPosition
scytheClone.Handle.Orientation = originalOrientation
end
-- Monitor the Workspace for Scythe removal
workspace.ChildRemoved:Connect(function(removedChild)
if removedChild.Name == "Scythe" then
wait(3)
respawnScythe()
end
end)
-- Monitor player removal to check for Scythe ownership
game.Players.PlayerRemoving:Connect(function(player)
if player.Backpack:FindFirstChild("Scythe") or
(player.Character and player.Character:FindFirstChild("Scythe")) then
wait(3)
respawnScythe()
end
end)
-- Monitor player deaths to respawn the Scythe
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character.Humanoid.Died:Connect(function()
if character:FindFirstChild("Scythe") then
wait(3)
respawnScythe()
end
end)
end)
end)