W001: Unknown global "player" script

I’m having trouble on line 35 of this script, where whenever I hover over “player” I keep getting the message “W001: Unknown global ‘player’”. Anyway I could fix that so the script can work again?

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local f = Instance.new("Folder", plr)
	f.Name = "leaderstats"
	local coins = Instance.new("IntValue", f)
	coins.Name = "Coins"
	coins.Value = 0
	local function onPlayerJoin(player)
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player
		coins.Parent = leaderstats
		
		local data
		local success, errormessage = pcall(function()
			data = myDataStore:GetASync(player.UserId.."-coins")
		end)
		
		if success then
			coins.Value = data
		else
			print ("There was an error while getting your data.")
			warn(errormessage)
		end
	end
	game.Players.PlayerAdded:Connect(onPlayerJoin)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-coins",player.leaderstats.Coins.Value)
	end)
	
	if success then
		print ("Player Data succesfully saved!")
	else
		print ("An error occured while saving your data.")
		warn(errormessage)
	end
	
end)
1 Like

Why did you add an OnPlayerAdded function inside of your already existing event connection?

3 Likes

The method PlayerRemoving executes has a parameter named plr, did you mean to use that with the save data?

PlayerRemoving:Connect(function(plr)
...
myDataStore:SetAsync(player.UserId.."-coins",...

Do you suggest that I should change plr to player?

Yeah, you can if you want to - although I recommend keeping it consistent, since your PlayerAdded method uses plr as well.
EDIT
Basically this

myDataStore:SetAsync(player.UserId.."-coins",player.leaderstats.Coins.Value)

to this

myDataStore:SetAsync(plr.UserId.."-coins",plr.leaderstats.Coins.Value)

That works, but whenever test it in game, grab coins, leave, and come back. They don’t stay.

Are you testing in studio? If so, make sure you have access to API services checked in game settings.

No, I’m testing it in the actual game, and before you say it, yes I do publish it to Roblox before going into the game.

2 Likes

That could be because of the issue @xKaihatsu mentioned before - you have a onPlayerJoin method inside of your PlayerAdded method, which will execute after the first player joins.
It’s a simple fix though, just remove the whole onPlayerJoin method (event connected to it included) and set up the GetAsync.

game.Players.PlayerAdded:Connect(function(plr)
    -- Assuming `coins` was set up
    local success, data = pcall(myDataStore.GetAsync, myDataStore, plr.UserId.."-coins")
    if success and data ~= nil then
        coins.Value = data
    else
        print("Error:", data)
    end
end)

EDIT

Did you add the code that makes the new leaderstats and Coins where the comment is?

If I add that to my script, then all the coins on the leaderboard just disappear.

That’s cool, but where would I add that at? I’m new to scripting and I have no idea on where to place that at.

You could replace your original PlayerAdded code with the code I’d written, with the necessary edits I’d mentioned previously.

So I’d have to replace the bit where it makes a leaderstats folder with the code you’ve typed?

1 Like