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

How can I change Flashlight color when a player touches a part?

Asked by 4 years ago
Edited 4 years ago

I want to change the players flashlight/torso point-light color when the player touches a part and I have been unsuccessful. When the player joins the game their flashlight is red and I am trying to make it white when they touch a part.

--Flashlight script 

function onPlayerEntered(newPlayer)
    repeat wait() until newPlayer.Character 
    local l = Instance.new("PointLight", newPlayer.Character:WaitForChild("Torso"))
    l.Brightness = 27
    l.Range = 20
    l.Color = Color3.new (170, 0, 0)
    l.Shadows = true
end

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

------------------------------------------------------------------------------------------------
--On touch brick attempt (Need help with this)
function onTouch(hit)
    local Block = game.Workspace.Block
    local l = Instance.new("PointLight", hit.Character:WaitForChild("Torso"))
    if hit.Parent:FindFirstChild("Humanoid") then
        l.Brightness = 27
        l.Range = 20
        l.Color = Color3.new (250, 250, 250)
        l.Shadows = true
    end

end

2 answers

Log in to vote
0
Answered by
Norbunny 555 Moderation Voter
4 years ago
Edited 4 years ago

Your for loop isn't going to dynamically check for new players. You want instead to listen to the PlayerAdded event.

local players = game:GetService("Players") -- the player service

players.PlayerAdded:Connect(onPlayerEntered) -- Connecting the function to the event

Regarding your brick touch attempt,

You don't need to change the brightness, range and everything again! You can just set the light's color to the new one! R15 characters do not have a "Torso" part, you might want to check for both "Torso" and "LowerTorso", a component of a R15 character.

local Block = game.Workspace.Block -- The part you want to listen to the touch of

function onPlayerEntered(newPlayer)
    repeat wait() until newPlayer.Character 
    local l = Instance.new("PointLight", newPlayer.Character:WaitForChild("Torso"))
    l.Brightness = 27
    l.Range = 20
    l.Color = Color3.new (170, 0, 0)
    l.Shadows = true
end

function onTouch(hit)
    -- finding the humanoid
    local humanoid = hit.Parent:FindFirstChild("Humanoid")

    if(humanoid) then
        local character = humanoid.Parent
        local torso = character:FindFirstChild("Torso") or character:FindFirstChild("LowerTorso")

        local l = torso:FindFirstChildOfClass("PointLight") -- Finding the light in the character's torso

        if(l) then
            l.Color = Color3.new (250, 250, 250)
        end
    end
end

-- Touched is the event fired when something touches the part
-- We connect our function to it
Block.Touched:Connect(onTouch)

With all the changes made, your code should look like this:

local players = game:GetService("Players") -- the player service

local Block = game.Workspace.Block -- The part you want to listen to the touch of

function onTouch(hit)
    -- finding the humanoid
    local humanoid = hit.Parent:FindFirstChild("Humanoid")

    if(humanoid) then
        local character = humanoid.Parent
        local torso = character:FindFirstChild("Torso") or character:FindFirstChild("LowerTorso")

        local l = torso:FindFirstChildOfClass("PointLight") -- Finding the light in the character's torso

        if(l) then
            l.Color = Color3.new (250, 250, 250)
        end
    end
end

players.PlayerAdded:Connect(onPlayerEntered) -- Connecting the function to the event

-- Touched is the event fired when something touches the part
-- We connect our function to it
Block.Touched:Connect(onTouch)

Your final code should look something like this:

local players = game:GetService("Players") -- the player service

local Block = game.Workspace.Block -- The part you want to listen to the touch of

function onPlayerEntered(newPlayer)
    repeat wait() until newPlayer.Character 
    local l = Instance.new("PointLight", newPlayer.Character:WaitForChild("Torso"))
    l.Brightness = 27
    l.Range = 20
    l.Color = Color3.new (170, 0, 0)
    l.Shadows = true
end

function onTouch(hit)
    -- finding the humanoid
    local humanoid = hit.Parent:FindFirstChild("Humanoid")

    if(humanoid) then
        local character = humanoid.Parent
            local torso = character:FindFirstChild("Torso") or character:FindFirstChild("LowerTorso")

            local l = torso:FindFirstChildOfClass("PointLight") -- Finding the light in the character's torso

        if(l) then
            l.Color = Color3.new (250, 250, 250)
        end
    end
end

-- Touched is the event fired when something touches the part
-- We connect our function to it
Block.Touched:Connect(onTouch)

players.PlayerAdded:Connect(onPlayerEntered) -- Connecting the function to the event

If you have any further questions, I'm happy to help!

Related documentation: PlayerAdded

1
Thank you for the reply and I am deeply sorry for deleting the question there is no excuse for that. On another note I am getting an error in the output (expected 'end' to close 'function' at line 3) at <eof> would you please tell me why this is and help me work through this? SilverishReign 75 — 4y
0
That's okay, I just found it weird that you'd have deleted it. And my bad, I coded it fully in the browser, so I missed an "end", I have edited the code to be fully working! My apologies! Norbunny 555 — 4y
0
I have another error and I will post it below. I would appreciate your help SilverishReign 75 — 4y
0
Will gladly look into it once you post it! Norbunny 555 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Here is the code I am not sure if its in the correct order so bear with me.

Output:

1) Workspace.Block.Torso Dynamic Lighting:13: attempt to index local 'l' (a nil value) Workspace.Block.Torso Dynamic Lighting', Line 13

2) ServerScriptService.Script:26: attempt to index local 'l' (a nil value) Script 'ServerScriptService.Script', Line 26

function onPlayerEntered(newPlayer)
    repeat wait() until newPlayer.Character 
    local l = Instance.new("PointLight", newPlayer.Character:WaitForChild("Torso"))
    l.Brightness = 27
    l.Range = 20
    l.Color = Color3.new (170, 0, 0)
    l.Shadows = true
end

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

local Block = game.Workspace.Block -- The part you want to listen to the touch of

function onTouch(hit)
    -- finding the humanoid
    local humanoid = hit.Parent:FindFirstChild("Humanoid")

    if(humanoid) then
            local character = humanoid.Parent
            local torso = character:FindFirstChild("LowerTorso") or character:FindFirstChild("LowerTorso")

        local l = torso:FindFirstChildOfClass("PointLight") -- Finding the light in the character's torso

        l.Color = Color3.new (250, 250, 250)
    end
end

-- Touched is the event fired when something touches the part
-- We connect our function to it
Block.Touched:Connect(onTouch)

0
Again, your for loop doesn't check for players joining your game, it only looks at already existing ones. Replace it with the code I shown you at the top with players.PlayerAdded Norbunny 555 — 4y
0
Would you mind showing me? 0_o SilverishReign 75 — 4y
0
I edited my answer once more with how your code should look like. Feel free to copy & paste it and see if it works. I've kept all the comments and explanations on it so that you can look at it at any time you need! Norbunny 555 — 4y
1
THANK YOU! Sorry for my lack of understanding but i'm getting there I suppose. SilverishReign 75 — 4y
0
Take your time! Norbunny 555 — 4y

Answer this question