Hello! In my game, I have round system that I think it unreliable because it breaks my leaderstats and makes them not spawn in sometimes. My leaderstats is not the thing breaking itself, it is for sure the round script. I have tried a variety of things but nothing! Please help me to make this code more reliable (It is server-side btw.)
One thing I found is that if I have a conditional statement before doing something with the leaderstats/datastore, it breaks. Like if I check if the player exists before doing a check to see if they have enough money, it doesn’t spawn in.
local roundLength = 30
local intermissionLength = 60
local InRound = game.ReplicatedStorage.InRound
local Seconds = game.ReplicatedStorage.Seconds
local LobbySpawn = game.Workspace.IntermissionTP
local GameAreaSpawn = game.Workspace.TileTP
local Roof = game.Workspace.Roof
local BaseParts = game.ReplicatedStorage.BaseParts:GetChildren()
local DataStore2 = require(1936396537)
local PlayersInGame = {}
InRound.Changed:Connect(function()
print(tostring(InRound.Value))
if InRound.Value == true then
for _, v in ipairs(game.Players:GetChildren()) do
local char = v.Character
char.HumanoidRootPart.CFrame = GameAreaSpawn.CFrame
table.insert(PlayersInGame, v.Name)
end
elseif InRound.Value == false then
for _, v in ipairs(game.Players:GetChildren()) do
local RoofBuxStore = DataStore2("RoofBux", v)
local winsStore = DataStore2("Wins", v)
local char = v.Character
char.HumanoidRootPart.CFrame = LobbySpawn.CFrame
if table.find(PlayersInGame, v.Name) then
RoofBuxStore:Increment(10, 0)
winsStore:Increment(1, 0)
end
end
table.clear(PlayersInGame)
end
end)
function MoveRoof()
Roof:SetPrimaryPartCFrame(CFrame.new(Vector3.new(math.floor(math.random(-140, -50) / 10 + 0.5) * 10, Roof.PrimaryPart.Position.Y, math.floor(math.floor(math.random(-30, 60) / 10 + 0.5) * 10))))
end
function roundTimer()
while wait() do
InRound.Value = false
Roof:SetPrimaryPartCFrame(CFrame.new(-95, 11, 15))
for i = intermissionLength, 1, -1 do
print("Intermission: "..tostring(i))
Seconds.Value = "Intermission: "..i.."s"
wait(1)
end
InRound.Value = true
for i = roundLength, 1, -1 do
print("Game: "..tostring(i))
Seconds.Value = "Round: "..i.."s"
if i/6 == math.floor(i/6) then
MoveRoof()
end
local chosenPart = BaseParts[math.random(#BaseParts)]:Clone()
chosenPart.Size = Vector3.new(10, math.random(2, 4), 10)
chosenPart.Position = Vector3.new(math.floor(math.random(-140, -50) / 10 + 0.5) * 10, 20, math.floor(math.floor(math.random(-30, 60) / 10 + 0.5) * 10))
chosenPart.BrickColor = BrickColor.new("Medium red")
chosenPart.Material = "Neon"
chosenPart.Transparency = 0.3
chosenPart.Parent = game.Workspace
chosenPart.Anchored = false
chosenPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:FindFirstChild("Humanoid"):TakeDamage(100)
chosenPart:Destroy()
local playerName = game.Players:GetPlayerFromCharacter(hit.Parent).Name
if table.find(PlayersInGame, playerName) ~= nil then
table.remove(PlayersInGame, table.find(PlayersInGame, playerName))
end
end
end)
wait(1)
if chosenPart ~= nil then
chosenPart:Destroy()
end
end
end
end
spawn(roundTimer)
Thanks for reading!