Dictionary returning nil

Basically I need to get a value from a dictionary, which matches with the Team’s name that the player is currently on.

print(Player.Team.Name) works perfectly fine, but even when using tostring() it does not seem to get the value from the dictionary and is returning nil instead.

Here is the script:

local Badges = {
	["SEK"] = "rbxassetid://13360522826",
	["Polizei"] = "rbxassetid://13610655205",
	["Autobahnpolizei"] = "rbxassetid://13610684536",
	["Feuerwehr"] = "rbxassetid://13610749945",
	["Rettungsdienst"] = "rbxassetid://13610764116",
	["THW"] = "rbxassetid://13610764116",
	["Zoll"] = "rbxassetid://13610809138"
}

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FunkFolder = ReplicatedStorage:FindFirstChild("Funk")
local MessageEvent = FunkFolder:WaitForChild("Message")

local teams = {"Autobahnpolizei", "Feuerwehr", "Polizei", "Rettungsdienst", "SEK", "THW", "Zoll"}

local TextService = game:GetService("TextService")
local function FilterMessage(MessageToFilter, UserID)
	TextService:FilterStringAsync(MessageToFilter, UserID, Enum.TextFilterContext.PublicChat)
end

local EventFolder = ReplicatedStorage:WaitForChild("Funk")
local MessageEvent = EventFolder:FindFirstChild("Message")

MessageEvent.OnServerEvent:Connect(function(Player, Channel, Message)
	print("Message received by server")
	print(Badges[tostring(Player.Team.Name)])
	MessageEvent:FireAllClients(Player, Channel, FilterMessage(Message, Player.UserId), Badges[tostring(Player.Team.Name)])
end)

Any help is appreciated!

1 Like
  • Make sure that the player really is in one of the teams indexed in dictionary
  • Player.Team.Name already returns a string, so there is not need for tostring()

Otherwise everything seems fine

I have checked that and everything seems fine, that’s why I do not know why I get nil when doing print(Badges[Player.Team.Name]).

Try storing it into a value?

if not Player["Team"] then
     return warn("No team assigned!")
end

local value = Player.Team.Name

print(value, Badges[value])

ALSO it could be that “Player.Team.Name” doesn’t exist in the dictionary you listed. Meaning whatever Team you’re trying to referencing you forgot to include it into the dictionary. And also check your spelling on the team names too, maybe you missed a letter or 2. VVVV

local Badges = {
	["SEK"] = "rbxassetid://13360522826",
	["Polizei"] = "rbxassetid://13610655205",
	["Autobahnpolizei"] = "rbxassetid://13610684536",
	["Feuerwehr"] = "rbxassetid://13610749945",
	["Rettungsdienst"] = "rbxassetid://13610764116",
	["THW"] = "rbxassetid://13610764116",
	["Zoll"] = "rbxassetid://13610809138"
}