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

How to make my LocalScript wait for the Character and working more than once?

Asked by
Seyfert 90
7 years ago
Edited 7 years ago

Okay guys hoping you can help me out here, I need this part

repeat wait() until player.Character ~= nil

in my LocalScript because it later complains about in Server log

-- character not being a valid member of Model

from this line

local character = player.Character

I understand why this is occurring, the script is running before the Character is created, that is why I have that repeat up there. The issue with the repeat is that, after the Character is no longer nil, the script will never re-run. So, if the player resets, the LocalScript is useless because of that statement because it won't run again. What else can I do?

Full Script

local player = script.Parent.Parent
repeat wait() until player.Character ~= nil
local character = player.Character
local humanoid = character.Humanoid
local repStorage = game:GetService("ReplicatedStorage")
local event = repStorage:WaitForChild("RemoteEvent") 

game.Players.LocalPlayer.Character.Humanoid.Health==0 do wait(); end
humanoid.Died:connect(function(Died)
    print('Checking if the boss dies')
    for i, v in pairs(game.Players:GetChildren()) do 
        if v.TeamColor == BrickColor.new('Bright red') and player.Name == v.Name then       
            event:FireServer()
        end
    end
end)


1
You could enclose all of your code in a CharacterAdded function :D User#11440 120 — 7y

1 answer

Log in to vote
0
Answered by
soved 69
7 years ago
Edited 7 years ago

Here this should work like a charm. Don't use a repeat loop for this kind of stuff.

local Plr = game:GetService("Players").LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:wait()

-- Bla Bla Bla Bla

Please learn how to organise your code. And also explain your problem better.

0
@soved, I think this is a good step in the right direction but the issue still persists sadly. After I reset the script no longer works. I think I am just going to have another script place this LocalScript in the character of the person. Seyfert 90 — 7y
Ad

Answer this question