Improving DataStore Script

I don’t usually use DataStoreService and I’m making a game that kinda needs DataStoreService so i’m wondering if there is anyway i could improve this script or make it more reliable

–Here’s The Script

local DataStore = game:GetService(“DataStoreService”)
local BadgeData = DataStore:GetDataStore(“BadgeData”)
local badge = 0

game.ReplicatedStorage.BedBadgeBind.Event:Connect(function ()
badge = 1
end)

local function DataSave(player)
BadgeData:SetAsync(tostring(player.UserId), badge)
end

local function LoadSave(player)
local badgeLoad = BadgeData:GetAsync(tostring(player.UserId))
return badgeLoad
end

game.Players.PlayerAdded:Connect(LoadSave)

game.Players.PlayerRemoving:Connect(DataSave)

2 Likes

Any function that uses an external API from the server should be wrapped in a pcall because they can fail at any time

2 Likes

Use ``` at the beginning of the code and the end in your message so its easier for the eyes.

Wrap your LoadSave and DataSave with pcall functions
When the player is new to the game obviously everything in his datastore is nil

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.