Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I give a green light to players on the green team?

I'm trying to make players on the green team glow with a green light, but when I do, I get the error "charname is not a valid member of Players". What i'm trying to do is have the script find the name of the player (from the model), then find the corresponding player in game.Players.

function onRespawn(Character)
	if (Character:findFirstChild("LowerTorso") ~= nil) then
		local charname = Character.Name		
		local playerteam = game.Players.charname.Team
		if playerteam == "Green" then		
			local light = Instance.new("PointLight")
			light.Parent = Character.LowerTorso
			light.Range = 15
			light.Brightness = 10
			light.Color = Color3.new(0,255,0)
		end
	end
	if (Character:findFirstChild("LowerTorso") == nil) then
	end	
end
game.Workspace.ChildAdded:connect(onRespawn)

2 answers

✓ Accepted answer

Try editing this:

game.Workspace.ChildAdded:connect(function(child)
	if(child:IsA("Model"))then
		local player = game.Players:GetPlayerFromCharacter(child);
		if(player ~= nil)then
			-- this model is a character
			if(player.Team == game.Teams.Green)then
				-- player is on the green team, make them glow
				local light = Instance.new("PointLight", child.Torso);
				light.Range = 15;
				light.Brightness = 10;
				-- Color3.new(r, g, b) takes three parameters between 0 and 1 (percentage of each color) whereas Color3.fromRGB(r, g, b) takes three parameters between 0 and 255
				light.Color = Color3.fromRGB(0, 255, 0);
			else
				-- player is not on the green team
			end
		end
	end
end)

4 comments

0

Doesn't work.

0

Answer edited; the value of player.Team is a Team Object, not a string, as I took from your original code. Next time, try editing the code. Fix was simple.

0

I did edit it and I though that was what you were checking for.

0

Fair enough

This worked. I modified a toolgiver to give pointlights instead, then I put it on the spawnpad.

local debounce = false

function getPlayer(humanoid) 
local players = game.Players:children() 
for i = 1, #players do 
if players[i].Character.Humanoid == humanoid then return players[i] end 
end 
return nil 
end 

function onTouch(part) 

local human = part.Parent:findFirstChild("Humanoid") 
if (human ~= nil) and debounce == false then

debounce = true

local player = getPlayer(human) 

if (player == nil) then return end 

local elight = part.Parent.LowerTorso:findFirstChild("PointLight")
if (elight == nil) then
	local light = script.Parent:clone()
	light.Parent = human.Parent.LowerTorso
	light.Enabled = true
end


wait(2)
debounce = false
end
end


script.Parent.Parent.Touched:connect(onTouch) 

This discussion is preserved as it was. New answers, comments, and votes are closed.