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

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

Asked by 9 years ago

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

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

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.

0
Thanks! I wasn't aware of the CharacterAdded event. :) RedneckBane 100 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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) 
Log in to vote
0
Answered by 9 years ago
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)

Log in to vote
0
Answered by 9 years ago
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!

0
Thanks! I wasn't aware of the CharacterAdded function. :) RedneckBane 100 — 9y

Answer this question