Hey developers! So this might be a very nonsensical question but I read through my code and I couldn’t spot anything wrong. If you can please let me know. I was making a DataStore for a game that I’m working on and I made it so that if the Data Saves, it prints a string, and if it fails it prints another string. (I followed this from the Wiki I believe). Feedback would be much appreciated, thanks and cheers!
To clarify: I should be seeing the strings printed in the output window but it’s completely blank.
Here’s the code written in the ServerScriptService on a Server Script.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("UniD")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = 0
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Parent = leaderstats
gems.Value = 0
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats
rebirths.Value = 0
local PlayerID = "PlayerID_"..player.userId
local DataFetching
local SuccessfulFetch, UnsuccessfulFetch = pcall(function()
local DataFetching = DataStore:GetAsync(player.UserId)
end)
if SuccessfulFetch then
coins.Value = DataFetching
gems.Value = DataFetching
rebirths.Value = DataFetching
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local PlayerID = "PlayerID_"..player.UserId
local CoinData = player.leaderstats.Coins.Value
local GemData = player.leaderstats.Gems.Value
local RebirthData = player.leaderstats.Rebirths.Value
local SuccessfulFetch, UnsuccessfulFetch = pcall(function()
DataStore:SetAsync(PlayerID, CoinData, GemData, RebirthData)
end)
if SuccessfulFetch then
print("I gots the Data! :D")
else
print("Oh noes, I failed to gets the data! D:")
warn(UnsuccessfulFetch)
end
end)
seems you redeclare Data fetching, local is for declaring variable inside the scope,
you can remove the local thing and you never used UnsuccessfulFetch. try add else iside the if statement block. And inside that else statement, print this print("Failed to fetch because of".. UnsuccessfulFetch)
Thank you for your advice, it’s quite helpful but I keep getting thrown an error when I replace the last lines of the player added function. See the below screenshot for the error. If you can tell me on how I can fix this would be greatly appreciated.
Extremely sorry for the late response, but I would just like to thank you for helping me out here. I was stuck on this code block for some time now and I tested out your rewritten version of the code and it worked. Thank you very much once again and have a good one.