#Code doesn't working

41 messages · Page 1 of 1 (latest)

past sail
#

I would like if a zombie dies it respawns after a few seconds.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

-- Function to respawn a zombie
local function respawnZombie(zombie)
task.wait(2) -- Wait for 2 seconds
local newZombie = zombie:Clone() -- Clone the zombie
newZombie.Parent = Workspace -- Place the new zombie back in the workspace
newZombie:SetPrimaryPartCFrame(zombie:GetPrimaryPartCFrame()) -- Set the position to the same as the original
end

-- Function to handle zombie destruction
local function onZombieDestroyed(zombie)
zombie:Destroy() -- Destroy the original zombie
respawnZombie(zombie) -- Respawn the zombie
end

-- Connect the destruction event to the handler
Workspace.ChildRemoved:Connect(function(child)
if child:IsA("Model") and child.Name == "Zombie" then -- Assuming zombies are models named "Zombie"
onZombieDestroyed(child)
end
end)

stark timber
#

It doesn't work because you canoot clone a model that has been destroyed

#

You need to put the asset of the zombie inside replicatedstorage and clone it from there after the zombie dies

stark timber
past sail
#

okay

agile agateBOT
#

studio** You are now Level 1! **studio

past sail
stark timber
past sail
# stark timber Let me see the script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

-- Function to respawn a zombie
local function respawnZombie(zombie)
task.wait(2) -- Wait for 2 seconds
local newZombie = zombie:Clone() -- Clone the zombie
newZombie.Parent = Workspace -- Place the new zombie back in the workspace
newZombie:SetPrimaryPartCFrame(zombie:GetPrimaryPartCFrame()) -- Set the position to the same as the original
end

-- Function to handle zombie destruction
local function onZombieDestroyed(zombie)
zombie:Destroy() -- Destroy the original zombie
respawnZombie(zombie) -- Respawn the zombie
end

-- Connect the destruction event to the handler
Workspace.ChildRemoved:Connect(function(child)
if child:IsA("Model") and child.Name == "Zombie" then -- Assuming zombies are models named "Zombie"
onZombieDestroyed(child)
end
end)

stark timber
#

Well at the function onZomeDestroyed

#

You first destroy the zombie and then you respawn it

#

but when you respawn it the model wont exist because you already destroyed it

past sail
#

What should the code be then?

stark timber
#

if you share the project with me i can rewrite it because i ll put the asset in replicatyedstorage

mint ore
#

can someone help me fr

stark timber
past sail
stark timber
#

k

past sail
stark timber
#

is it markus

past sail
agile agateBOT
#

studio** You are now Level 2! **studio

stark timber
#

k accepted

past sail
stark timber
past sail
stark timber
past sail
stark timber
#

ok

#

Done

past sail
stark timber
#

yes

#

i can show you if you want

agile agateBOT
#

studio** You are now Level 4! **studio

past sail
stark timber
#

np

past sail
# stark timber np

Another question, could you create a script for me so that he doesn't attack other zombies?

stark timber
#

Later