How to save players team?

Hi,

I am making an obby game and whenever they leave I want it to set the teams data.

However, this doesn’t work as it gives me the error of attempt to index nil with Name

How would I get the player’s team? Help, please!

local succ, err = pcall(function()
		teamData:SetAsync(plr.UserId.."-team", plr.Team.Name)
	end)

i would have to see more of your code to see whats going on here

alright, here it is:

local dss = game:GetService("DataStoreService")

local deathData = dss:GetDataStore("deathData")
local teamData = dss:GetDataStore("teamData")


game.Players.PlayerAdded:Connect(function(plr)
	plr.Neutral = true
	
	local lb = Instance.new("Folder", plr)
	lb.Name = "leaderstats"

	local deaths = Instance.new("IntValue", lb)
	deaths.Name = "Deaths"
	
	--deaths loading
	
	local death
	
	local succ, err = pcall(function()
		death = deathData:GetAsync(plr.UserId.."-deaths")
	end)
	
	if succ then
		deaths.Value = death
	else
		warn("Error with getting deaths: " ..err)
	end
	
	--stage loading
	
	local team
	
	local succ, err = pcall(function()
		team = teamData:GetAsync(plr.UserId.."-team")
	end)
	
	if succ then
		plr.Team = game.Teams:FindFirstChild(team)
	else
		warn("Error with getting team: " ..err)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	--deaths
	local succ, err = pcall(function()
		deathData:SetAsync(plr.UserId.."-deaths", plr.leaderstats.Deaths.Value)
	end)
	
	if succ then
		print("Deaths successfully saved.")
	else
		warn("Error with saving deaths: " ..err)
	end
	
	--stage
	local succ, err = pcall(function()
		teamData:SetAsync(plr.UserId.."-team", plr.Team.Name)
	end)

	if succ then
		print("Deaths successfully saved.")
	else
		warn("Error with saving stage: " ..err)
	end
end)

im not entirely sure why its not working so maybe try defining the team that they are on as a variable at the beginning of the PlayerRemoving function and see if it works then

im trying to set the players team to lobby when they are being removed, but it is saying attempt to index nil with Name

game.Players.PlayerRemoving:Connect(function(plr)
	--deaths
	print(plr.Team.Name, plr.Team)
	plr.Team = "Lobby"

lol you have to put the teams object, not the name of the team

so you would do plr.Team = game:GetService("Teams").Lobby

ohh lol, ill try that now.

ok so it appears that I leave before the game can set my team to lobby.

Also whenever I join the team I am on is automatically in neutral, and not lobby or whatever team they were on. I have made sure that lobby is auto assignable, but yet it does not work

found the source of the problem

1 Like

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