Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I watched a video from AlvinBlox but something in my code is wrong .what is wrong with it?

i cant figure out what.I should have spawned in spawn1 but I spawn in spawn2 and the code that is meant to save data to datastore doesn't save it.plz help me.

Here is the code:

local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("ObbyDataStore")

game.Players.PlayerAdded:Connect(function(player)

local data

local success, errorMessage = pcall(function()
	data = DataStore:GetAsync(player.UserId.."-stage")
end)
if success then
	
	if data then
	player.Team = game.Teams[data]
	else
		player.Team = game.Teams.stage1
	end
else
	player.Team = game.Teams.stage1
end
	player:LoadCharacter()
end)

	game.Players.PlayerRemoving:Connect(function(player)
	
local TeamName = game.player.Team.Name

local success, errorMessage = pcall(function()
	DataStore:SetAsync(player.UserId.."-stage",TeamName)
end)
end)

Plz help me fix it.

2 comments

0

ohh now i get it

0

Please indent your code properly

1 answer

you put game.player in TeamName, thats why it isnt saving, do this

game.Players.PlayerRemoving:Connect(function(player)
	local TeamName = player.Team.Name

	local success,errormessage = pcall(function()
		DataStore:SetAsync(player.UserId.."-stage",TeamName)
	end
end)

5 comments

0

bro i didnt understand

0

you basically put TeamName = game.player,but game.player doesn't exist

0

its player because you already defined the player in playerRemoving because thats the player you want to save, the player that is leaving

0

accept this answer if it helped :D

0

oh now i get it.thanks soooo much bro

This discussion is preserved as it was. New answers, comments, and votes are closed.