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
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