Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

NPC Respawn Script, Only Spawning once? Fix?

Hello all! I am creating an NPC Respawn script that calls the ReplicatedStorage service of ROBLOX. It then is supposed to use the :Clone() function from ReplicatedStorage and place it according to the position of a Part in the workspace!

The name of the part is "ElfSwordsmanSpawn", and it seems to function great the first time. After it respawns the NPC once, it breaks. Is there a way to fix this code? Or is there something wrong ?

Here is my code, this is a Server Script in the ServerScriptService

local spawnLocation = game.Workspace:FindFirstChild("ElfSwordsmanSpawn") --Where you want the NPC to spawn

while true do
    wait(1)
    if not game.Workspace:FindFirstChild("ElfSwordsman") then--Npc name here 
        local CloneNpc = game.ReplicatedStorage:WaitForChild("ElfSwordsman"):Clone()
        CloneNpc.Parent = game.Workspace
        CloneNpc.Position = Vector3.new(0.175, 14.275, -13.4)
    end
end

2 comments

0

make usre ur deleting the npc. also, replicated storage disables scripts im pretty sure

0

if it's an NPC then it's gonna be a model so .Position won't work, if this is the case then that's the problem, it'll error and not run again. To fix this, use CloneNpc.HumanoidRootPart.Position = Vector3.new(0.175, 14,275, -13.4)

1 answer

A simple way to move a whole model is by adding a PrimaryPart to the model and then using :MoveTo() to teleport your model to any position. In character models, the PrimaryPart is usual assigned to the HumanoidRootPart. Also, place all the models that you want to clone in ServerStorage for safe keeping. Here's my code:

while wait(1) do
    if not game.Workspace:FindFirstChild("ElfSwordsman") then

        local CloneNpc = game:GetService('ServerStorage'):WaitForChild("ElfSwordsman"):Clone()
	local SpawnLocation = game.Workspace:FindFirstChild("ElfSwordsmanSpawn") 

        CloneNpc.Parent = game.Workspace
        CloneNpc:MoveTo(spawnLocation.Position)

    end
end

1 comment

0

if its a character the humanoidrootpart should be the primary part

This discussion is preserved as it was. New answers, comments, and votes are closed.