0% found this document useful (0 votes)
21 views3 pages

Roblox Pet Data Management Script

This document outlines a Lua script for managing player pets in a game using Roblox's DataStoreService. It includes functionalities for adding, removing, and retrieving pet data, as well as updating the user interface and handling player events. The script also implements an auto-save feature to ensure pet data is saved periodically and upon player exit or game closure.

Uploaded by

gokuboniewiem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Roblox Pet Data Management Script

This document outlines a Lua script for managing player pets in a game using Roblox's DataStoreService. It includes functionalities for adding, removing, and retrieving pet data, as well as updating the user interface and handling player events. The script also implements an auto-save feature to ensure pet data is saved periodically and upon player exit or game closure.

Uploaded by

gokuboniewiem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

local DataStoreService = game:GetService("DataStoreService")

local Players = game:GetService("Players")


local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Create DataStore
local PetDataStore = DataStoreService:GetDataStore("PlayerPets")

-- Create events for client-server communication


local RemoteEvents = [Link]("Folder")
[Link] = "PetEvents"
[Link] = ReplicatedStorage

local AddPetEvent = [Link]("RemoteEvent")


[Link] = "AddPet"
[Link] = RemoteEvents

local RemovePetEvent = [Link]("RemoteEvent")


[Link] = "RemovePet"
[Link] = RemoteEvents

local GetPetsEvent = [Link]("RemoteFunction")


[Link] = "GetPets"
[Link] = RemoteEvents

-- New event for updating UI


local UpdatePetUIEvent = [Link]("RemoteEvent")
[Link] = "UpdatePetUI"
[Link] = RemoteEvents

-- Player data cache


local playerPets = {}

-- Functions to manage pets


local function getPetData(userId)
if not playerPets[userId] then
local success, data = pcall(function()
return PetDataStore:GetAsync(userId) or {}
end)

if success then
playerPets[userId] = data
else
warn("Failed to load pet data for", userId, data)
playerPets[userId] = {}
end
end

return playerPets[userId]
end

local function savePetData(userId)


if playerPets[userId] then
local success, err = pcall(function()
PetDataStore:SetAsync(userId, playerPets[userId])
end)

if not success then


warn("Failed to save pet data for", userId, err)
end
end
end

local function addPet(userId, petId)


local pets = getPetData(userId)

if not pets[petId] then


pets[petId] = 1
else
pets[petId] = pets[petId] + 1
end

return pets
end

local function removePet(userId, petId)


local pets = getPetData(userId)

if pets[petId] then
pets[petId] = pets[petId] - 1

if pets[petId] <= 0 then


pets[petId] = nil
end
end

return pets
end

-- Function to update UI for a player


local function updatePetUI(player)
local userId = tostring([Link])
local pets = getPetData(userId)
UpdatePetUIEvent:FireClient(player, pets)
end

-- Handle player events


[Link]:Connect(function(player)
local userId = tostring([Link])
getPetData(userId) -- Load data when player joins

-- Add a short delay to ensure client is ready


wait(1)
updatePetUI(player)
end)

[Link]:Connect(function(player)
local userId = tostring([Link])
savePetData(userId) -- Save data when player leaves
playerPets[userId] = nil -- Clear from memory
end)

-- Connect remote events


[Link]:Connect(function(player, petId)
local userId = tostring([Link])
addPet(userId, petId)
updatePetUI(player)
end)
[Link]:Connect(function(player, petId)
local userId = tostring([Link])
removePet(userId, petId)
updatePetUI(player)
end)

[Link] = function(player)
local userId = tostring([Link])
return getPetData(userId)
end

-- Auto-save system
spawn(function()
while wait(300) do -- Save every 5 minutes
for userId in pairs(playerPets) do
savePetData(userId)
end
end
end)

-- Handle game closing


game:BindToClose(function()
for userId in pairs(playerPets) do
savePetData(userId)
end
end)

You might also like