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

attempt to index upvalue player a nil value why?

Asked by 5 years ago

I can't change team when touched a part my code:

local player = game.Players.LocalPlayer

script.Parent.Touched:Connect(function()
    player.TeamColor = BrickColor.new("Bright blue")
end)

1 answer

Log in to vote
0
Answered by 5 years ago

If the script is not a local script, Put this in a server script (Regular script) because you can take a LocalPlayer only from a local script.

If the script is a local script, Try using this script:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then -- If the thing who touched the part is a player
        local player = game.Players:FindFirstChild("hit.Parent.Name") -- Get the player

        player.TeamColor = BrickColor.new("Bright blue")
    end
end)

If any of what I said isn't working, Try using RemoteEvents:

http://robloxdev.com/api-reference/event/RemoteEvent/OnServerEvent or https://wiki.roblox.com/index.php?title=API:Class/RemoteEvent

Video about RemoteEvents and how they work:

https://www.youtube.com/watch?v=4Dc_bri9mjs

If this helped you please accept my answer that would be really appreciated, Plus we both make reputation out of that.

-MajinBluee

0
Use :GetPlayerFromCharacter(). Line 3 would only look for a player named "hit.Parent.Name". xPolarium 1388 — 5y
0
Do not remind users to accept your answer, as it is nature for them to accept it. You're just being greedy. User#19524 175 — 5y
Ad

Answer this question