Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why wont my event run more than once?

Asked by 5 years ago
Edited 5 years ago
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

0
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. 129Steve129 7 — 5y
0
Use CharacterAdded. MArzalAlBuchariZ 33 — 5y
0
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, 129Steve129 7 — 5y
0
steve im confused its in serverscriptserivce Boindoin -7 — 5y
View all comments (3 more)
0
sorry im confused with both of you can you copy my code then change it then send the message for a example? Boindoin -7 — 5y
0
C H A R A C T E R A D D E D Zottic 19 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
0
"You don't require the spaces when using `..` in a string" It makes it cleaner to read, you dont `require` it but its suggested. You could apply the same arguments from indenting to this. EpicMetatableMoment 1444 — 5y
Ad

Answer this question