Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 7 years ago

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

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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)
0
Doesn't work. JarFullOfMayonnaise 48 — 7y
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. KidTech101 376 — 7y
0
I did edit it and I though that was what you were checking for. JarFullOfMayonnaise 48 — 7y
0
Fair enough KidTech101 376 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

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) 

Answer this question