Hey all
I have a game where players can buy buttons from a shop, or ‘hatch’ special ones from a pet hatch system sort of thing. How this works is the buttons are in the shop gui, just not showing. When a player ‘hatches’ a button, a boolvalue inside of the button model (called ‘Shown’) becomes true, and shows in the shop. I have figured out how to save this fine, but with this comes another problem.
Now I have it set up where the Shown boolvalue is updated on the client, but this data needs to be sent to the server to store in the datastore. because of this, if a player hatches a button that nobody else in the server has, everybody in the server would get that button, when it should only be the player who hatched it. When I fix this glitch, the buttons don’t save properly. It’s basically just a neverending loop of error.
hopefully that made sense. If you don’t understand, I can simplify it for you.
Here are the scripts that handle this stuff:
ClientScript:
-- Client Script
local RS = game:GetService("ReplicatedStorage")
local updateShownValueRemote = RS:WaitForChild("UpdateShownValue")
-- Function to notify the server of a Shown value change
local function notifyServerOfChange(buttonName, newValue)
updateShownValueRemote:FireServer(buttonName, newValue)
end
-- Set up listeners for Shown value changes
local function setupShownValueListener()
for _, button in pairs(RS.Buttons:GetChildren()) do
local shownValue = button:FindFirstChild("Shown")
if shownValue and shownValue:IsA("BoolValue") then
-- Use a local copy of the value to prevent broadcasting to all clients
local localShownValue = Instance.new("BoolValue")
localShownValue.Name = shownValue.Name
localShownValue.Value = shownValue.Value
shownValue:GetPropertyChangedSignal("Value"):Connect(function()
localShownValue.Value = shownValue.Value
notifyServerOfChange(button.Name, localShownValue.Value)
end)
end
end
end
setupShownValueListener()
ServerScript:
-- Server Script
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local shownValuesDataStore = DataStoreService:GetDataStore("adsadasdew3qdasdqw")
-- RemoteEvent for handling Shown value updates
local updateShownValueRemote = Instance.new("RemoteEvent")
updateShownValueRemote.Name = "UpdateShownValue"
updateShownValueRemote.Parent = RS
-- Function to save a single Shown value
local function saveShownValue(player, buttonName, value)
local success, errorMessage = pcall(function()
local data = shownValuesDataStore:GetAsync(player.UserId) or {}
data[buttonName] = value
shownValuesDataStore:SetAsync(player.UserId, data)
end)
if success then
print("Successfully saved Shown value for button " .. buttonName .. " for player " .. player.Name)
else
warn("Failed to save Shown value for button " .. buttonName .. " for player " .. player.Name .. ": " .. errorMessage)
end
end
-- Handle the event when the client notifies of a Shown value change
updateShownValueRemote.OnServerEvent:Connect(function(player, buttonName, newValue)
local button = RS.Buttons:FindFirstChild(buttonName)
if button then
local shownValue = button:FindFirstChild("Shown")
if shownValue and shownValue:IsA("BoolValue") then
-- Only update the value on the server
shownValue.Value = newValue
saveShownValue(player, buttonName, newValue)
else
warn("Shown BoolValue not found for button: " .. buttonName)
end
else
warn("Button not found in ReplicatedStorage: " .. buttonName)
end
end)
-- Load the player's Shown values when they join
Players.PlayerAdded:Connect(function(player)
local success, data = pcall(function()
return shownValuesDataStore:GetAsync(player.UserId)
end)
if success and data then
for buttonName, value in pairs(data) do
local button = RS.Buttons:FindFirstChild(buttonName)
if button then
local shownValue = button:FindFirstChild("Shown")
if shownValue and shownValue:IsA("BoolValue") then
-- Set the value for the specific player
shownValue.Value = value
else
warn("Shown BoolValue not found for button: " .. buttonName)
end
else
warn("Button not found in ReplicatedStorage: " .. buttonName)
end
end
print("Successfully loaded data for " .. player.Name)
else
if not success then
warn("Failed to load data for " .. player.Name .. ": " .. tostring(data))
else
print("No data found for " .. player.Name)
end
end
end)
sorry if this code is a mess.
thanks for any help.