Can anyone tell me why this script clones two monsters? I should only clone one. Sometimes it clones up to four monsters at the same time.
local serverStorage = game:GetService("ServerStorage")
local barbarian = serverStorage:WaitForChild("Barbarian"):Clone()
local spawnpos = barbarian:WaitForChild("SpawnPosition")
spawnpos.Value = script.Parent.Position --This is part of another script, must stay in.
barbarian:PivotTo(script.Parent.CFrame * CFrame.Angles(0, math.rad(0), 0)) --Rotate 90 degree on spawn cframe 0 left 90 forward 180 right
local respawntime = 5
barbarian.Parent = workspace
barbarian.Humanoid.HealthChanged:Connect(function(newHealth)
if newHealth <= 0 then
wait(5)
barbarian:Destroy()
task.wait(respawntime) --change to task.wait it's more accurate than wait (optional)
script.Disabled = true
script.Disabled = false
end
end)
Thanks
Not sure if it’ll fix it, but try renaming the cloned monster so it doesn’t have the same name as the one in ServerStoarge.
Just do barbarian.Name = "GameBarbarian"
Look at this script, it works fine and only spawns one of the monsters. I can’t see a difference that would cause that, can you?
local serverStorage = game:GetService("ServerStorage")
local darkelf = serverStorage:WaitForChild("Darkelf"):Clone()
darkelf.HumanoidRootPart.Position = script.Parent.Position
local lighting = game:GetService("Lighting")
local spawnpos = darkelf:WaitForChild("SpawnPosition")
spawnpos.Value = script.Parent.Position
local respawntime = 120
local nightstart = 7
local nightend = 6
repeat wait(1) until lighting.ClockTime > nightstart or lighting.ClockTime < nightend
darkelf.Parent = game.Workspace.Attackable
darkelf.Humanoid.HealthChanged:Connect(function(newHealth)
if newHealth <= 0 then
wait(1)
darkelf:Destroy()
wait(respawntime)
script.Disabled = true
script.Disabled = false
end
end)
Hm. The only reason that could cause a double-clone would be having 2 (or more) models with the same name. Don’t have any other idea.
maybe I need a wait timer after the spawn. It might just be going too fast.
Instead of using Humanoid.HealthChanged, use Humanoid.Died. This will only fire once upon death, preventing the risk of multiple spawning. You could also add a debounce to the current function instead.
This is crazy. There is only one barbarian in the storage and it sometimes pops out four of them. But the darkelf spawn works perfect every time.
I fixed it by modifying the second script that was working. No idea why the first one was broken.