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)
7 comments
Well first of all having lua code inside every NPC would lag it up a lot and you could control it all from one script with the collection service also for keeping the code make sure that the code isnt parented to anything you delete from the NPC.
Use CharacterAdded.
you could also use a module and add a function like by doing --function modue.new end module would be the name of the table in the script that is returned from the module back to the script that requested it,
steve im confused its in serverscriptserivce
sorry im confused with both of you can you copy my code then change it then send the message for a example?
https://developer.roblox.com/api-reference/event/Player/CharacterAdded
C H A R A C T E R A D D E D