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 5 years ago
Edited 5 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.

01--Flashlight script
02 
03function onPlayerEntered(newPlayer)
04    repeat wait() until newPlayer.Character
05    local l = Instance.new("PointLight", newPlayer.Character:WaitForChild("Torso"))
06    l.Brightness = 27
07    l.Range = 20
08    l.Color = Color3.new (170, 0, 0)
09    l.Shadows = true
10end
11 
12for _,pl in pairs(game.Players:GetPlayers()) do
13    onPlayerEntered(pl)
14end
15 
View all 28 lines...

2 answers

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

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

1local players = game:GetService("Players") -- the player service
2 
3players.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.

01local Block = game.Workspace.Block -- The part you want to listen to the touch of
02 
03function onPlayerEntered(newPlayer)
04    repeat wait() until newPlayer.Character
05    local l = Instance.new("PointLight", newPlayer.Character:WaitForChild("Torso"))
06    l.Brightness = 27
07    l.Range = 20
08    l.Color = Color3.new (170, 0, 0)
09    l.Shadows = true
10end
11 
12function onTouch(hit)
13    -- finding the humanoid
14    local humanoid = hit.Parent:FindFirstChild("Humanoid")
15 
View all 30 lines...

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

01local players = game:GetService("Players") -- the player service
02 
03local Block = game.Workspace.Block -- The part you want to listen to the touch of
04 
05function onTouch(hit)
06    -- finding the humanoid
07    local humanoid = hit.Parent:FindFirstChild("Humanoid")
08 
09    if(humanoid) then
10        local character = humanoid.Parent
11        local torso = character:FindFirstChild("Torso") or character:FindFirstChild("LowerTorso")
12 
13        local l = torso:FindFirstChildOfClass("PointLight") -- Finding the light in the character's torso
14 
15        if(l) then
View all 25 lines...

Your final code should look something like this:

01local players = game:GetService("Players") -- the player service
02 
03local Block = game.Workspace.Block -- The part you want to listen to the touch of
04 
05function onPlayerEntered(newPlayer)
06    repeat wait() until newPlayer.Character
07    local l = Instance.new("PointLight", newPlayer.Character:WaitForChild("Torso"))
08    l.Brightness = 27
09    l.Range = 20
10    l.Color = Color3.new (170, 0, 0)
11    l.Shadows = true
12end
13 
14function onTouch(hit)
15    -- finding the humanoid
View all 34 lines...

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 — 5y
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 — 5y
0
I have another error and I will post it below. I would appreciate your help SilverishReign 75 — 5y
0
Will gladly look into it once you post it! Norbunny 555 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 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

01function onPlayerEntered(newPlayer)
02    repeat wait() until newPlayer.Character
03    local l = Instance.new("PointLight", newPlayer.Character:WaitForChild("Torso"))
04    l.Brightness = 27
05    l.Range = 20
06    l.Color = Color3.new (170, 0, 0)
07    l.Shadows = true
08end
09 
10for _,pl in pairs(game.Players:GetPlayers()) do
11    onPlayerEntered(pl)
12end
13 
14local Block = game.Workspace.Block -- The part you want to listen to the touch of
15 
View all 32 lines...
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 — 5y
0
Would you mind showing me? 0_o SilverishReign 75 — 5y
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 — 5y
1
THANK YOU! Sorry for my lack of understanding but i'm getting there I suppose. SilverishReign 75 — 5y
0
Take your time! Norbunny 555 — 5y

Answer this question