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

How do you fix a script that inserts something into a player?

I'm trying to insert a Pointlight into each new player that joins the game. For some reason, it isn't working.

local light = Instance.new('PointLight')

function new(plr)
	local Character = plr:WaitForChild('Character')
	if plr~=nil then
		light.Parent = Character:FindFirstChild('Torso')
		light.Shadows = true
		light.Range = 15
		light.Color = Color3.new(255,255,255)
	end
end
game.Players.PlayerAdded:connect(new)

4 answers

✓ Accepted answer

You would want to use the CharacterAdded event, which fires every time you respawn.

local light = Instance.new('PointLight')

function new(plr)
    plr.CharacterAdded:connect(function(char) --char is your character when it comes up.
        light.Parent = Character:WaitForChild('Torso') --Use the wait for child method to have the script yield until Torso comes up.
        light.Shadows = true
        light.Range = 15
        light.Color = Color3.new(255,255,255)
    end)
end
game.Players.PlayerAdded:connect(new)

Your script should work now.

1 comment

0

Thanks! I wasn't aware of the CharacterAdded event. :)

Fixed this should work

local light = Instance.new('PointLight') 
function new(plr) 
    if plr~=nil then 
		while plr.Character == nil do
			wait(0.5)
		end
        light.Parent = plr.Character:FindFirstChild('Torso') 
        light.Shadows = true 
        light.Range = 15 
        light.Color = Color3.new(255,255,255) 
    end 
end 
game.Players.PlayerAdded:connect(new) 
function onPlayerEntered(newPlayer)
	repeat wait() until newPlayer.Character	
	local l = Instance.new("PointLight", newPlayer.Character:WaitForChild("Torso"))
	l.Brightness = 15
	l.Range = 15
	while true do
		wait(1)
		l.Color = Color3.new(math.random(),math.random(),math.random())
	end
end

for _,pl in pairs(game.Players:GetPlayers()) do 
	onPlayerEntered(pl) 
end 

game.Players.ChildAdded:connect(onPlayerEntered)

local light = Instance.new('PointLight')

game.Players.PlayerAdded:connect(function(plr)
	local Character = plr:WaitForChild('Character')
	if Character then
		light.Parent = Character:FindFirstChild('Torso')
		light.Shadows = true
		light.Range = 15
		light.Color = Color3.new(255,255,255)
	end
end)

That's a slightly simpler way to do it. The only thing you should have really done is change if plr~=nil then to if Character~=nil then. That could be shortened to if Character then which is my line 5.

Feel free to comment with any questions. Accept/Voteup if you find it helpful. Thanks!

1 comment

0

Thanks! I wasn't aware of the CharacterAdded function. :)

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