Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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 comment

1

You could enclose all of your code in a CharacterAdded function :D

1 answer

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.

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.