Hello there, it sure has been a long time since I have asked peoples in the devforum for help fix my roblox studio scripts, I will make it kinda quick because my head is not feeling good after using Roblox Studio’s in-built AI script helper and ChatGPT’s Script support for more then hours straight and still not getting an correct fixes…
-
What do you want to achieve? - I want this long lines of script that I took some notes from This youtube video for me to make an Roblox Experience inspired by another roblox experience called “Sol’s RNG”, I used a bunch of AI script code helps from in-built from studio and chat gpt pretty much a lot…
-
What is the issue? - the problem is that when player joins the game and makes an progresses in the experience (such as rolling for Auras such as Goog, Rare and such), the game script in the ServerScriptService wouldn’t load back the Data of player whenever they rejoin… I do not know how can I fix this issue yet… but here is the LUA script code of it and the rbxm game file in case you do want to view what is wrong…
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RollEvent = ReplicatedStorage:WaitForChild("RollEvent")
local Chances = require(ReplicatedStorage:WaitForChild("Chances"))
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerDataStore")
local Inventory = {}
-- Function to save player data to DataStore
local function SavePlayerData(User)
local userId = User.UserId
local success, err = pcall(function()
local rolls = User:FindFirstChild("leaderstats"):FindFirstChild("Rolls").Value
local saveData = {Inventory = Inventory[userId], Rolls = rolls}
-- Update the existing data in the DataStore instead of overwriting it
local existingData = playerDataStore:GetAsync("PlayerData"..userId) or {}
for key, value in pairs(saveData) do
existingData[key] = value
end
playerDataStore:SetAsync("PlayerData"..userId, existingData)
end)
if not success then
warn("Failed to save data for player "..userId..": "..err)
end
end
-- Function to handle player join
local function PlayerJoined(User)
-- Step 1: Create the leaderstats folder and parent it to the User
local LeaderStats = Instance.new("Folder", User)
LeaderStats.Name = "leaderstats"
-- Step 2: Create the Rolls IntValue inside the leaderstats folder
local Rolls = Instance.new("IntValue", LeaderStats)
Rolls.Name = "Rolls"
-- Attempt to load the player's data
local userId = User.UserId
local success, data = pcall(function()
return playerDataStore:GetAsync("PlayerData"..userId)
end)
-- Handle the retrieved or default data
if success then
if data then
Inventory[userId] = data.Inventory or {
Limit = 10,
LatestRoll = nil,
Equipped = nil,
Backpack = {}
}
-- Load the Rolls value
local rollsValue = data.Rolls or 0
Rolls.Value = rollsValue
else
Inventory[userId] = {
Limit = 10,
LatestRoll = nil,
Equipped = nil,
Backpack = {}
}
Rolls.Value = 0 -- Default Rolls value if no data is found
end
else
-- Log an error if data retrieval failed
warn("Failed to load data for player "..userId)
end
end
-- Function to handle player leaving
local function PlayerLeft(User)
SavePlayerData(User)
end
-- Connect events
Players.PlayerAdded:Connect(PlayerJoined)
Players.PlayerRemoving:Connect(PlayerLeft)
local SelectRNG = require(ReplicatedStorage:WaitForChild("SelectRNG"))
RollEvent.OnServerInvoke = function(User, Header)
local UserInventory = Inventory[User.UserId]
local UserLimit = UserInventory["Limit"]
local Backpack = UserInventory["Backpack"]
if Header == "Get" then
if #Backpack >= UserLimit then
table.remove(Backpack, table.find(Backpack, UserInventory["Equipped"]) or UserLimit)
end
table.insert(Backpack, UserInventory["LastestRoll"])
elseif Header == "Waste" then
UserInventory["LastestRoll"] = nil
elseif Header == "GetInventory" then
return Backpack
else
local Result = Chances[SelectRNG(User)][1]
UserInventory["LastestRoll"] = Result
return Result
end
print(Backpack)
return
end
SolsRNGLikeGameTest1.rbxl (72.2 KB)
- What solutions have you tried so far? - I have used Roblox Studio’s In-Built AI Script Advice/Explain code system and ChatGPT website in the attempt of solving that issue, but things did not go well…
So please let me know whenever you found the issue or want to discuss this Topic About, my eyesight is kind of blurry from scripting for hours straight, I will go take a rest now