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
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
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)