Answered by
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.
1 | local players = game:GetService( "Players" ) |
3 | players.PlayerAdded:Connect(onPlayerEntered) |
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.
01 | local Block = game.Workspace.Block |
03 | function onPlayerEntered(newPlayer) |
04 | repeat wait() until newPlayer.Character |
05 | local l = Instance.new( "PointLight" , newPlayer.Character:WaitForChild( "Torso" )) |
08 | l.Color = Color 3. new ( 170 , 0 , 0 ) |
14 | local humanoid = hit.Parent:FindFirstChild( "Humanoid" ) |
17 | local character = humanoid.Parent |
18 | local torso = character:FindFirstChild( "Torso" ) or character:FindFirstChild( "LowerTorso" ) |
20 | local l = torso:FindFirstChildOfClass( "PointLight" ) |
23 | l.Color = Color 3. new ( 250 , 250 , 250 ) |
30 | Block.Touched:Connect(onTouch) |
With all the changes made, your code should look like this:
01 | local players = game:GetService( "Players" ) |
03 | local Block = game.Workspace.Block |
07 | local humanoid = hit.Parent:FindFirstChild( "Humanoid" ) |
10 | local character = humanoid.Parent |
11 | local torso = character:FindFirstChild( "Torso" ) or character:FindFirstChild( "LowerTorso" ) |
13 | local l = torso:FindFirstChildOfClass( "PointLight" ) |
16 | l.Color = Color 3. new ( 250 , 250 , 250 ) |
21 | players.PlayerAdded:Connect(onPlayerEntered) |
25 | Block.Touched:Connect(onTouch) |
Your final code should look something like this:
01 | local players = game:GetService( "Players" ) |
03 | local Block = game.Workspace.Block |
05 | function onPlayerEntered(newPlayer) |
06 | repeat wait() until newPlayer.Character |
07 | local l = Instance.new( "PointLight" , newPlayer.Character:WaitForChild( "Torso" )) |
10 | l.Color = Color 3. new ( 170 , 0 , 0 ) |
16 | local humanoid = hit.Parent:FindFirstChild( "Humanoid" ) |
19 | local character = humanoid.Parent |
20 | local torso = character:FindFirstChild( "Torso" ) or character:FindFirstChild( "LowerTorso" ) |
22 | local l = torso:FindFirstChildOfClass( "PointLight" ) |
25 | l.Color = Color 3. new ( 250 , 250 , 250 ) |
32 | Block.Touched:Connect(onTouch) |
34 | players.PlayerAdded:Connect(onPlayerEntered) |
If you have any further questions, I'm happy to help!
Related documentation: PlayerAdded