local player = game:GetService("Players").LocalPlayer local status = game.ReplicatedStorage.StatusBar:WaitForChild("Status") local intermissionLengthInSeconds = game.ReplicatedStorage.StatusBar.IntermissionTime.Value --codeblock 1 function LobbyStart() for i = 1,intermissionLengthInSeconds do wait(1) intermissionLengthInSeconds = intermissionLengthInSeconds - 1 status.Value = "Intermission " .. intermissionLengthInSeconds end --codeblock 2 if intermissionLengthInSeconds < 1 then status.Value = "Round In Progress" game.Workspace.Lobby.Lobby.SpawnLocation.Position = Vector3.new(-376.163, 20.572, 48.294) intermissionLengthInSeconds = 10 end end LobbyStart() -- codeblock 3 game.Workspace.NPCs.Innocent.Humanoid.Died:Connect(function() status.Value = "Intermission " .. intermissionLengthInSeconds game.Workspace.Lobby.Lobby.SpawnLocation.Position = Vector3.new(-415.437, -160.351, 63.309) game.Workspace:FindFirstChildOfClass("Model").Head:Destroy() LobbyStart() end)
at codeblock 3 How do I make the event run every time the NPC die's, cause when the NPC die's he respawn's in a new model so the code will stay connected with the dead NPC. if you know what I mean
GENERAL PRACTICE
Use :WaitForChild()
to make sure a part or model exists before defining or changing it.
Use :GetService()
to retrieve the ReplicatedStorage
Service
Use workspace
rather than game.Workspace
You don't require the spaces when using ..
in a string
ISSUES
Defining "intermissionLengthInSeconds" as a value procludes you from changing it afterwards since your initial definition is a pre-set number, so you need to use .Value
to be able to change it afterwards.
As you said, your problem is that the code is only linking to a single NPC, the one that starts in the game. However, I propose using the ChildAdded
event to create the Died Event by checking that a new child (presumably a clone) is added to the "NPCs" Folder / Model and that the "child" is named "Innocent"
In order for the death function to work for the first "Innocent" you can add the model to ReplicatedStorage
and clone it into the "NPCs" Folder / Model when the player first joins the game.
REVISED LOCAL SCRIPT
local player = game:GetService("Players").LocalPlayer local bar = game:GetService("ReplicatedStorage"):WaitForChild("StatusBar") local status = bar:WaitForChild("Status") local length = bar:WaitForChild("IntermissionTime") local spawnloc = workspace:WaitForChild("Lobby"):WaitForChild("Lobby"):WaitForChild("SpawnLocation") local npcfolder = workspace:WaitForChild("NPCs") local function LobbyStart() local clone = game:GetService("ReplicatedStorage"):WaitForChild("Innocent"):Clone() clone.Parent = npcfolder for i = 1, length.Value do wait(1) length.Value = length.Value - 1 status.Value = ("Intermission "..length.Value) end if length.Value < 1 then status.Value = "Round In Progress" spawnloc.Position = Vector3.new(-376.163, 20.572, 48.294) length.Value = 10 end end LobbyStart() npcfolder.ChildAdded:Connect(function(child) if child.Name == "Innocent" and child:IsA("Model") then child:WaitForChild("Humanoid").Died:Connect(function() status.Value = ("Intermission "..length.Value) spawnloc.Position = Vector3.new(-415.437, -160.351, 63.309) LobbyStart() child:Destroy() end) end end)
I presume the :Destroy() on a model was to kill the player, but if you use the :Clone()
function this wouldn't be required since the clone automatically is recreated.
If it isn't then you won't require line 33 and can move the cloning outside of the local function like so
local player = game:GetService("Players").LocalPlayer local bar = game:GetService("ReplicatedStorage"):WaitForChild("StatusBar") local status = bar:WaitForChild("Status") local length = bar:WaitForChild("IntermissionTime") local spawnloc = workspace:WaitForChild("Lobby"):WaitForChild("Lobby"):WaitForChild("SpawnLocation") local npcfolder = workspace:WaitForChild("NPCs") local clone = game:GetService("ReplicatedStorage"):WaitForChild("Innocent"):Clone() clone.Parent = npcfolder local function LobbyStart() for i = 1, length.Value do wait(1) length.Value = length.Value - 1 status.Value = ("Intermission "..length.Value) end if length.Value < 1 then status.Value = "Round In Progress" spawnloc.Position = Vector3.new(-376.163, 20.572, 48.294) length.Value = 10 end end LobbyStart() npcfolder.ChildAdded:Connect(function(child) if child.Name == "Innocent" and child:IsA("Model") then child:WaitForChild("Humanoid").Died:Connect(function() status.Value = ("Intermission "..length.Value) spawnloc.Position = Vector3.new(-415.437, -160.351, 63.309) workspace:FindFirstChildOfClass("Model"):WaitForChild("Head"):Destroy() LobbyStart() end) end end)