I am trying to make a new mob spawn after this one is dead.
Ideally I would like there to be a cooldown timer like 10-20 seconds, then the mob would respawn in a specific location (most likely with a place holder block)
Basically what is happening is that when a player clicks the “Mob” it takes damage, and when it reaches 0 HP, it sets the health of the Humanoid to 0, killing it. I am unsure what to do once this event happens though.
I have looked at some previous instances of this on the forum and tried their solutions but none of it seems to work, the Mob will just keep sitting there dead and not moving.
I have provided the code that makes the mob take damage (its a bit janky, I am still learning) and below that is a screenshot of what is actually in the Mob model.
Thank you for all your help
local HP = "1000"
local mob = game.Workspace.Mob.Torso
local damage = game.ReplicatedStorage.damage.Value
local clickDetector = game.Workspace.Mob.ClickDetector
local healthNumber = mob.healthbargui.barbackground.healthamount
local healthBar = mob.healthbargui.barbackground.bar
local healthValue = Instance.new("IntValue")
healthValue.Value = HP --Monster HP
healthNumber.Text = HP
healthValue:GetPropertyChangedSignal("Value"):Connect(function()
healthNumber.Text = healthValue.Value
print(healthNumber.Text, healthValue.Value)
end)
function onMouseClick(player)
-- Subtract healthValue by Damage Amount and Change the healhBar Size
healthValue.Value -= damage
healthBar.Size -= UDim2.new(damage/HP, 0, 0, 0) --damage/whatever the monster HP is
if healthValue.Value <= 0 then
clickDetector.MaxActivationDistance = UDim2.new(0)
print("stopped")
healthNumber.Text = "0"
player.leaderstats.Coins.Value += math.random(10, 20)
print("Coins Added")
game.Workspace.Mob.Humanoid.Health = 0
print("Died")
end
print(healthNumber.Text, healthValue.Value)
end
clickDetector.MouseClick:Connect(onMouseClick)
I have copied a duplicate over to the Replicated Storage. Pardon my ignorance but im not exactly sure what code i need to clone it though when the original mob reaches 0 HP. that is the main thing I need help with. Thank you for your advice thus far though!
You must have the original in serverStorage or replicated storage.
Then if the character dies, you clone a new one from the oroginal and you head to the workspace, and to the position of the old (dead) model and the old one you erase.
I’d write a code, but I can’t do it on my phone and translator.
First, create a script [Server script] name it Respawn (or whatever you want)
put that script inside your npc (mob)
Now check if your npc is died
local npc = script.Parent
local humanoid = npc.Humanoid
humanoid.Died:Connect(function()
-- Moving from here soon
end)
Now clone the mob
local clonedMob = script.Parent:Clone()
Parent your cloned mob in workspace
clonedMob.Parent = workspace
Now make joints for your cloned mob
clonedMob:MakeJoints()
Now destroy your dead mob
script.Parent:Destroy()
Joining everything together!
local mob = script.Parent
local humanoid = mob.Humanoid
local clonedMob = script.Parent:Clone()
local respawnTime = 0 -- Added respawn time, the amount takes mob to respawn, set this to 0 for instant respawn
humanoid.Died:Connect(function()
task.wait(respawnTime or 0)
clonedMob.Parent = workspace
clonedMob:MakeJoints()
script.Parent:Destroy()
end)
Thank you for the help, This almost worked perfectly, but it seems that the scripts that made the HP and movement not work once the mob respawns, do you know why this may be happening?
no like everything breaks. the health bar displays its default “Health” text instead of the HP number, it wont move, and the bar doesnt go down when clicked. Not sure if this is a mistake in my code (probs) or if it has to do with cloning.
I think the issue may be that the code needs to be called again when it is cloned? im pretty new to cloning so not exactly sure how it works
Honestly I’d just clone a copy from ServerStorage like @BroOfNoob suggested. ReplicatedStorage is not a good place to put npcs as their health, and other properties can be seen by exploiters.
You’re just going to do what @Synitx showed you, except put a clone of the mob into ServerStorage.
local mob = script.Parent
local humanoid = mob.Humanoid
local clonedMob = game.ServerStorage.Mobs[mob.Name]:Clone()
local respawnTime = 0 -- Added respawn time, the amount takes mob to respawn, set this to 0 for instant respawn
humanoid.Died:Connect(function()
task.wait(respawnTime or 0)
clonedMob.Parent = workspace
clonedMob:MakeJoints()
script.Parent:Destroy()
end)
it seems like the same thing is happening still. once the mob respawns i get an error saying "Humanoid is not a valid member of Model “Mob” "
my assumption is that the code is being ran before the mob actually gets cloned which in turn breaks the script? its a bit hard to explain over just text
Update: After doing a test, it looks like the script for the health just doesnt even run, i believe in this case i would have to check for the Died event to make the whole script run is that correct?
you can use mob:WaitForChild(“Humanoid”) instead of mob.Humanoid. If it yields then the problems either where you parented your script, or there’s no humanoid in the mob at least not one named ‘Humanoid’.
Disable then re-enable the scripts in the mob.
Add a new line after this
and type
for i,v in pairs(clonedMob:GetChildren()) do
if v:IsA("Script") or v:IsA("LocalScript") then
v.Disabled = true
delay(0.1,function() v.Disabled = false end)
end
end
Stop being worried about your models being stolen. When they reach Workspace, they can be stolen anyway.
Use ReplicatedStorage for everything you want replicated. (that is the reason we do have it in the first place). Let me know if this next point is wrong: All objects ect. in ReplicatedStorage will be loaded by the client gradually, without taking hit in performance at all, all done while they’re playing! So models and textures will load in almost instant!
Now,
If you use ServerStorage, the object will first start to load when you start cloning it from ServerStorage to ect. workspace. How can this be bad? It can be bad because textures, 3D models and such might be delayed due to their render-data not loaded yet.
People still can easily store your model, because it clones from replicated or server storage
then it clones and have parent of workspace, exploiter can easily get your mob from workspace
(Wrong reply)
I have been following this thread in hopes of solving my own NPC issue… please post a solution the correct way so that others can see how it has been solved. Writing “solved” in the title doesn’t help anyone else with the same problem. Please mark the reply that had solved your issue as “solution” or if you solved it another way, please be so kind as to post that solution in a reply to yourself, and mark that as the “solution” so we can also benefit from it. Thanks