Unknown Global Player; DataStores

Im trying to save Obby Stage Stats but im keep getting unknown global ‘player’
heres the code:

	local DataStore = game:GetService("DataStoreService"):GetDataStore("Stage")
    local defaultstage = 1 
    local playersLeft = 0


game.Players.PlayerAdded:Connect(function(player)
	
	playersLeft = playersLeft + 1
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = 1
	stage.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.WalkSpeed = 16
		
	end)

end)

local player_data

pcall(function()
	player_data = DataStore:GetAsync(player.UserId.."-Stage")
end)

if player_data ~= nil then
	stage.Value = player_data
	
 else
	stage.Value = defaultstage
end

local bindablEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		DataStore:SetAsync(player.UserId.."-Stage",player.leaderstats.Stage.Value)
		print("Saved DS for "..player.Name.." ")
	end)
	
	playersLeft = playersLeft - 1
	bindablEvent:Fire()
end)
game:BindToClose(function()
	while playersLeft > 0 do
		bindablEvent.Event:Wait()
	end
end)

Are you sure it’s coming from this script? Show the full error w/ line number so we can help you bettter.

oh wait in the first pcall you have a random “player” variable

You’re getting data from the play outside the scope of the variable ‘player’. (you’re getting data from the player outside the PlayerAdded function???)

This should work.

local DataStore = game:GetService("DataStoreService"):GetDataStore("Stage")
local defaultstage = 1 
local playersLeft = 0
game.Players.PlayerAdded:Connect(function(player)
	playersLeft = playersLeft + 1
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = 1
	stage.Parent = leaderstats
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.WalkSpeed = 16
	end)
    local player_data
    pcall(function()
	    player_data = DataStore:GetAsync(player.UserId.."-Stage")
    end)
    if player_data ~= nil then
	    stage.Value = player_data
    else
	    stage.Value = defaultstage
    end
end)
local bindablEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		DataStore:SetAsync(player.UserId.."-Stage",player.leaderstats.Stage.Value)
		print("Saved DS for "..player.Name.." ")
	end)
	playersLeft = playersLeft - 1
	bindablEvent:Fire()
end)
game:BindToClose(function()
	while playersLeft > 0 do
		bindablEvent.Event:Wait()
	end
end)
3 Likes

Thank you so much! What did you do btw?

I simply moved where it gets the data for the player into the player added function.

i.e

from

game.Players.PlayerAdded:Connect(function(player)
    
end)
-- get player data (errors because player is undefined)

to

game.Players.PlayerAdded:Connect(function(player)
    -- get player data
end)
1 Like