Okay so, I was working on a nametag system, and it was all going well until I tried messing with saving the nametags, I cannot find the problem.
I don’t need to have this feature, but I want to have it.
I’m new to datastores, so I don’t know if this is just a beginner mistake or something, but I’ve tried everything I could and cannot find the problem.
I’m not getting any errors or anything, it’s just not saving or loading anything.
Also, it’s printing on player connected but not on player removing. Which is weird because I’m leaving by going to the menu and leaving, not just stopping the game in studio.
I have API access enabled for studio too, but it didn’t work in game or in studio.
Here’s the script:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("NameForDataStore") -- I'm gonna change this btw
game.Players.PlayerAdded:Connect(function(player)
local data
local char = player.Character or player.CharacterAdded:Wait()
print("player connected")
local success, errorMessage = pcall(function()
data = DataStore:GetAsync(player.UserId.."-name")
print(player.UserId.."-name")
print(data)
print("got player data")
end)
if success then
if data then
char.Head:WaitForChild("Name"):WaitForChild("TextLabel").Text = data
print("synced player data")
else
char.Head:WaitForChild("Name"):WaitForChild("TextLabel").Text = player.Name
print("player has no data")
end
else
char.Head:WaitForChild("Name"):WaitForChild("TextLabel").Text = player.Name
print("failed to get data")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
local pName = char.Head:WaitForChild("Name"):WaitForChild("TextLabel").Text
print("got player nametag")
print(pName)
local success, errorMessage = pcall(function()
DataStore:SetAsync(player.UserId.."-name", pName)
print("Saved player name")
end)
if success then
print("Success")
else
print(errorMessage)
end
end)
Try this in Roblox Player, not Studio. Studio shuts down servers, so if you want to use this in studio you’ll have to make use of game:BindToClose()
1 Like
I did try in Roblox player, still didn’t work sadly
Alright, try using game:BindToClose()
game:BindToClose(function()
for _, Player in (game.Players:GetPlayers()) do
-- Save their data
end
end)
Okay, do I just put my current code in that for loop?
or put the part I save at in the for loop?
No, put the BindToClose as a separate event, like you have game.Players.PlayerAdded
I got the error “attempt to call a table value” at the for loop line, I swear I know how to fix this on my own but my brain is dead and I forgot
this line btw: for _, Player in game.Players:GetPlayers() do
Ah, sorry, made a mistake. It’s meant to be pairs(game.Players:GetPlayers())
Well, its probably just my bad computer, but it froze my studio, so hold on technical difficulties.
1 Like
okay it finally stopped dying, I got the error “Not running script because past shutdown deadline (x11)”
If I’m being honest, don’t know what that means.
Strange. Could I see your code?
sure
local DataStore = DataStoreService:GetDataStore("NameForDataStore") -- I'm gonna change this btw
game.Players.PlayerAdded:Connect(function(player)
local data
local char = player.Character or player.CharacterAdded:Wait()
print("player connected")
local success, errorMessage = pcall(function()
data = DataStore:GetAsync(player.UserId.."-name")
print(player.UserId.."-name")
print(data)
print("got player data")
end)
if success then
if data then
char.Head:WaitForChild("Name"):WaitForChild("TextLabel").Text = data
print("synced player data")
else
char.Head:WaitForChild("Name"):WaitForChild("TextLabel").Text = player.Name
print("player has no data")
end
else
char.Head:WaitForChild("Name"):WaitForChild("TextLabel").Text = player.Name
print("failed to get data")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
local pName = char.Head:WaitForChild("Name"):WaitForChild("TextLabel").Text
print("got player nametag")
print(pName)
local success, errorMessage = pcall(function()
DataStore:SetAsync(player.UserId.."-name", pName)
print("Saved player name")
end)
if success then
print("Success")
else
print(errorMessage)
end
end)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local pName = char.Head:WaitForChild("Name"):WaitForChild("TextLabel").Text
print("got player nametag")
print(pName)
local success, errorMessage = pcall(function()
DataStore:SetAsync(player.UserId.."-name", pName)
print("Saved player name")
end)
if success then
print("Success")
else
print(errorMessage)
end
end
end)
… Okay, why are you using game.Players.LocalPlayer in a server script? The for loop already gives you the Player variable.
This is what I mean… my brain is dead, I forgot it was a server script. Let me fix that rq
edit: its fixed
It… works now? Like the entire thing.
I don’t know what fixed it, but thank you
1 Like
Nvm, it doesnt work in a regular game 
Is there any errors in output?
No, it’s loading the data now but still not saving it.
I can’t find the problem so I’m probably going to scrap saving for now and come back to it when I’m a better programmer.
Alright. Your code seems fine too.