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")
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.
local Block = game.Workspace.Block
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)
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")
if(l) then
l.Color = Color3.new (250, 250, 250)
end
end
end
Block.Touched:Connect(onTouch)
With all the changes made, your code should look like this:
local players = game:GetService("Players")
local Block = game.Workspace.Block
function onTouch(hit)
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")
if(l) then
l.Color = Color3.new (250, 250, 250)
end
end
end
players.PlayerAdded:Connect(onPlayerEntered)
Block.Touched:Connect(onTouch)
Your final code should look something like this:
local players = game:GetService("Players")
local Block = game.Workspace.Block
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)
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")
if(l) then
l.Color = Color3.new (250, 250, 250)
end
end
end
Block.Touched:Connect(onTouch)
players.PlayerAdded:Connect(onPlayerEntered)
If you have any further questions, I'm happy to help!
Related documentation: PlayerAdded
4 comments
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 would you please tell me why this is and help me work through this?
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!
I have another error and I will post it below. I would appreciate your help
Will gladly look into it once you post it!