DataStore not saving teams, no errors. Not printing result

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make datastore that save teams.
  2. What is the issue? Include screenshots / videos if possible!
    Data store not saving a player team.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    No yet.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local DSS = game:GetService("DataStoreService")
local StageData = DSS:GetDataStore("StageData")

game.Players.PlayerAdded:Connect(function(plr)
	local data
	local success, err = pcall(function()
		data = StageData:GetAsync(plr.UserId)
	end)
	if success and data then
		-- A player has data.
		print("Got data.")
		   plr.Team = data
	elseif success and not data then
		-- New player.
		print("Got no data.")
		plr.Team = game:GetService("Teams").Stage1
	else
		-- Error.
		warn("Team data store failed: " .. tostring(err))
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local team = plr.Team
	print(team)
	local data
	local success, err = pcall(function()
		data = StageData:GetAsync(plr.UserId)
	end)
	if success and data then
		print("Player has data! (on saving)")
		-- Player has data. Updating Data.
		StageData:UpdateAsync(plr.UserId, function(oldstage)
			local newstage = team
			if newstage then
				return newstage -- Updated value.
			else
				newstage = team 
				if newstage then
					return newstage -- Updated value.
				else
					return nil -- Cancels update because it doesn't contain value.
				end
				end
		end)
	elseif success and not data then
		print("Player has no data (on saving)")
		-- Newbie player leaves. Setting data.
		local success, err = pcall(function()
			StageData:SetAsync(plr.UserId, team)
		end)
		if success then
			print("Successfully save data player: " .. plr.Name)
		else
			warn("Failed to save player data! " .. plr.Name .. " error: " .. tostring(err))
		end
	end
end)

it works on saving method. (It because i put more print function, im probably getting confused why.), but on getting team doesn’t work. (Sometimes saving method broken.)

I give up already. (This post was closed.) (Another chance)

I recommend using DataStore2, it prevents Data Loss and it always saves the data. Also, don’t give up that easy, lol!

I have been fixing 3 times, no works.

Anyways, thanks for responding.

Ah yes, DataStore2. I don’t know if datastore2 support with teams. If so, in the future, i will try again fixing my team data store.

Have you tried saving the team’s name?

Yes, I do. already using print(team), it return team name. but it doesn’t continue code.

I already give up about this. :pensive:

No, I meant the .Name property of the team instance. As well, you should never give up. (:

Already save by name properties, no works.

It could be that the server closed before the data saved. Try adding :BindToClose().

game:BindToClose(function()
    -- save data
end)
2 Likes

I will try that to see if it work or not.

It work, but when getting data:

it show this:
image

When im tryping print data, it said Stage4.

I think i should use FindFirstChild method.

Mhm, you should use :FindFirstChild.

Thank you for helping me. It works.

1 Like

I have one problem left.

It show me this error:

I added Team.Name but it doesn’t work. It keep saying this error.

@COUNTYL1MITS

Nevermind, I already fixed it.