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)
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?