How would I get the player's team color and name onto this tool?

  1. What do you want to achieve? I am attempting to rename a script so I can get the tool name and color into this keycard.

  2. What is the issue? For some reason it gives me an error and doesn’t do it.

  3. What solutions have you tried so far?
    local Players = game:GetService(“Players”)
    local Player = Players.LocalPlayer

script.Parent.Text = Player.Team.Name
script.Parent.TextColor3 = Player.TeamColor.Color
script:Destroy()

The script above is what I am using. It was changed from a hella long one because it still wasn’t working but I’m there now.

1 Like

This may help, but it can also help in debugging. If warn() doesn’t show in the console, just print().

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

-- Ensure the player and team properties are valid
if Player and Player.Team then
    -- Set the text to the player's team name if it exists
    if Player.Team.Name then
        script.Parent.Text = Player.Team.Name
    else
        warn("Player's team does not have a name")
    end

    -- Set the text color to the team's color if it exists
    if Player.Team.TeamColor then
        script.Parent.TextColor3 = Player.Team.TeamColor.Color
    else
        warn("Player's team does not have a TeamColor property")
    end
else
    warn("Player or Player.Team is nil")
end

-- Destroy the script after execution to clean up
script:Destroy()
1 Like

A local script runs before any other script so the player might not have a team set when your script runs.

1 Like

So I checked out this script and now it’s replying with, warn(“Player or Player.Team is nil”). Any way you can think of a fix for this?

1 Like

The script only runs after the tool is given to them.

1 Like

When is the tool given to them?

1 Like

Sorry for the late response. The tool is only given when they do :give me. So hence the confusion.

1 Like

Try this,

local Tool = script.Parent
local Players = game:GetService("Players")
local Player,Team = nil,nil

Tool.Equipped:Connect(function()
Player = Players.LocalPlayer
Team = Player.Team.Name
end)
1 Like

So I did try that, had to fix the script.parent. Otherwise, it still didn’t work. Came back with an error.
Players.Bjorn_Kronos.Backpack.Level-1.Handle.SurfaceGui.Frame.Team.TeamSet:7: attempt to index nil with ‘Team’ - Server - TeamSet:7
00:17:38.847 Script ‘Players.Bjorn_Kronos.Backpack.Level-1.Handle.SurfaceGui.Frame.Team.TeamSet’, Line 7 - Studio - TeamSet:7

This is the screenshot of the tool to see if that would help out.

Are you sure that the script is a localscript?

Just checked the image, your script Isn’t a LocalScript… and Player.LocalPlayer only works with LocalScripts

Use this instead

local Tool = script.Parent
local Players = game:GetService("Players")
local Player,Team = nil,nil

Tool.Equipped:Connect(function()
if Players:GetPlayerFromCharacter(Tool.Parent) == nil then return end

Player = Players:GetPlayerFromCharacter(Tool.Parent)
Team = Player.Team.Name

end)
1 Like

This worked. Thank you a lot man!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.