So, I have an issue with syncing a server and local value. We have a server script that adds all datastore values in the form of regular number/bool values. It looks like this:
local statsList = {}
local function createValue(player, key, value)
local valueType = typeof(value)
local newValue
if valueType == "number" then
newValue = Instance.new("NumberValue")
elseif valueType == "string" then
newValue = Instance.new("StringValue")
elseif valueType == "boolean" then
newValue = Instance.new("BoolValue")
else
return -- Skip unsupported types
end
newValue.Name = key
newValue.Value = value
newValue.Parent = statsList[player] -- Parent the value to the player's folder AAAAAAAAAAAAAAAAAAAA
end
-- Function to create values based on the profile data
local function createValues(player, profile)
if not profile.Data then return end
-- Create a folder to hold the player's stats
local statsFolder = Instance.new("Folder")
statsFolder.Name = "Stats"
statsFolder.Parent = player
statsList[player] = statsFolder
-- Iterate through the profile data and create corresponding values in the player's folder
for key, value in pairs(profile.Data) do
createValue(player, key, value)
end
end
local Players = game:GetService("Players")
local PlayerDataHandler = require(game.ServerScriptService.PlayerDataHandler)
Players.PlayerAdded:Connect(function(plr)
local profile = PlayerDataHandler:getProfile(plr)
if profile then
createValues(plr, profile)
end
end)
Players.PlayerRemoving:Connect(function(plr)
statsList[plr] = nil
end)
-- Refreshing the values
while task.wait(0.1) do
for player, playerStats in pairs(statsList) do
local profile = PlayerDataHandler:getProfile(player)
if profile and profile.Data then
for _, v in pairs(playerStats:GetChildren()) do -- Iterate through stats folder's children
if profile.Data[v.Name] ~= nil then
v.Value = profile.Data[v.Name] -- Refresh the value in the stats folder with the profile's values
end
end
end
end
end
And now, we have sand that spawns on a field, and there’s a cap of how much of it can spawn. Here’s the sand spawn script:
function SandSpawn()
while true do
task.wait(1 / currentSpawnRate.Value)
if currentSandSpawned.Value < currentCap.Value then
local x = zone.Position.X + math.random(-zone.Size.X/2,zone.Size.X/2)
local y = zone.Position.Y
local z = zone.Position.Z + math.random(-zone.Size.Z/2,zone.Size.Z/2)
local NewPart = sands[math.random(1, #sands)]:Clone()
NewPart.Name = "SandPiece"
NewPart:PivotTo(CFrame.new(Vector3.new(x,y,z)))
NewPart.Parent = game.Workspace.SpawnedSandpieces
RemoteSandSpawn:FireServer()
end
end
end
task.spawn(SandSpawn)
Now, the problem is that if the spawn speed is really fast, the CurrentSandSpawned value can’t update fast enough, so the sand goes over the cap. I have thought of doing something where for every sand spawn I fire a remote event from server to client, but as you can probably guess at high spawn speeds this can get very laggy, so I’d like to hear if there’s any good solution for this.