Hello there, I have just finished making the stats and incrementers for my GUI player stats and when I open a server in studio stats are duplicated to every user for some reason I have seen threads on the forum saying to put “player” in the functions I am using and I wonder why it does not work. I also am wondering if it is how I am handling my table and where I put it I will provide the codes for it down below.
What I want my code to do is have the stats update separately and not as a group of players so each player has their own cash rebirths and whatever else I could add as a stat afterwards.
Thanks in advance!
– Scripteor
Server Sided handler (ServerScriptService)
local Players = game:GetService("Players") -- Players
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- ReplicatedStorage
local CashUpdater = ReplicatedStorage:WaitForChild("CashUpdater") -- CashUpdater Event
local RebirthUpdater = ReplicatedStorage:WaitForChild("RebirthUpdater")
local PlayerList = {} -- Player list (Table)
local Stats = {
Cash = 0,
Increaser = 1,
Rebirths = 0,
RebirthPrice = 100
}
local function InitPlayerStats(Player) -- Initialize player stats
PlayerList[Player] = Stats -- Puts the Stats inside the Players
local leaderstats = Instance.new("Folder") -- Initialize the Stats
leaderstats.Name = "leaderstats"
local Cash = Instance.new("NumberValue")
Cash.Name = "Cash"
Cash.Value = 0
Cash.Parent = leaderstats
local Rebirths = Instance.new("NumberValue")
Rebirths.Name = "Rebirths"
Rebirths.Value = 0
Rebirths.Parent = leaderstats
local RebirthPrice = Instance.new("NumberValue")
RebirthPrice.Name = "RebirthPrice"
RebirthPrice.Value = 100
RebirthPrice.Parent = leaderstats
local Increaser = Instance.new("NumberValue")
Increaser.Name = "Increaser"
Increaser.Value = 1
Increaser.Parent = leaderstats
local EXP = Instance.new("NumberValue")
EXP.Name = "EXP"
EXP.Value = 0
EXP.Parent = leaderstats
leaderstats.Parent = Player -- Sets the Players as the parent of the Stats
end
local function UpdateCash(Player) -- The function that adds cash
local Stats = PlayerList[Player] -- Sets the stats in the player list for the function
Stats.Cash = Stats.Cash + Stats.Increaser -- Increases Stats
local leaderstats = Player:FindFirstChild("leaderstats") -- Finds leaderstats of player
local PlayerCash = leaderstats:FindFirstChild("Cash") -- Finds Cash of player
PlayerCash.Value = Stats.Cash -- Sets the Cash stat in the Table for the player
end
local function UpdateRebirths(Player) -- Function that runs on event
local Stats = PlayerList[Player]
if Stats.Cash >= Stats.RebirthPrice then
Stats.Cash = Stats.Cash - Stats.RebirthPrice
Stats.Rebirths = Stats.Rebirths + 1
Stats.Increaser = Stats.Increaser + 1
Stats.RebirthPrice = math.floor(Stats.RebirthPrice * 2.5 / 1.2) else
end
end
for _, Player in Players:GetPlayers() do -- Function that goes through players to initialize
InitPlayerStats(Player) -- function above
end
game.Players.PlayerAdded:Connect(function(Player) -- Function that adds Players on join
InitPlayerStats(Player) -- function above
end)
game.Players.PlayerRemoving:Connect(function(Player) -- Function that removes Players on leave
PlayerList[Player] = nil -- Reset the value in the Player table
end)
CashUpdater.OnServerEvent:Connect(function(Player) -- Starts the event
UpdateCash(Player) -- Function launched on event
end)
RebirthUpdater.OnServerEvent:Connect(function(Player) -- Starts the event
UpdateRebirths(Player) -- Function launched on event
end)
Client Side CashButton Handler (Under the appropriate GUI)
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- Replicated Storage
local Players = game:GetService("Players") -- Players
local CashUpdater = ReplicatedStorage:WaitForChild("CashUpdater") -- CashUpdater Event
local Player = Players.LocalPlayer -- Local Player
local Button = script.Parent -- Cash Button
local TextLabel = script.Parent.Parent.CashAmount -- Cash Label
local Cash = Player:WaitForChild("leaderstats"):WaitForChild("Cash") -- Finds the Cash value for the local player
Button.MouseButton1Click:Connect(function() -- On click CashButton function
CashUpdater:FireServer() -- Fires the event to the server @ ServerScriptService.StatsHandler
end)
Cash:GetPropertyChangedSignal("Value"):Connect(function() -- Detects the value has changed
TextLabel.Text = Cash.Value .. " Cash"
print(Cash.Value)
end)
Client Sided RebirthButton Handler (Under the appropriate GUI)
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- Services and remote events
local Players = game:GetService("Players")
local RebirthUpdater = ReplicatedStorage:WaitForChild("RebirthUpdater")
local Player = Players.LocalPlayer -- Variables
local RebirthPriceLabel = script.Parent
local CashIncreaserLabel = script.Parent.Parent.CashButton
local RebirthAmountLabel = script.Parent.Parent.RebirthAmount
local CashLabel = script.Parent.Parent.CashAmount
local Rebirths = Player:WaitForChild("leaderstats"):WaitForChild("Rebirths") -- Data for the player
local RebirthPrice = Player:WaitForChild("leaderstats"):WaitForChild("RebirthPrice")
local Increaser = Player:WaitForChild("leaderstats"):WaitForChild("Increaser")
local Cash = Player:WaitForChild("leaderstats"):WaitForChild("Cash")
-- Single use functions for UI
local function CashUiUpdater()
CashLabel.Text = Cash.Value .. " Cash"
end
local function RebirthUiUpdater()
RebirthAmountLabel.Text = Rebirths.Value .. " Rebirths"
end
local function RebirthPriceUiUpdater()
RebirthPriceLabel.Text = RebirthPrice.Value .. " Cash to rebirth"
end
local function IncreaserUiUpdater()
CashIncreaserLabel.Text = "+" .. Increaser.Value .. " Cash"
end
-- Single use functions for UI
RebirthPriceLabel.MouseButton1Click:Connect(function() -- Mouse Clicked the rebirth button
if Cash.Value >= RebirthPrice.Value then
RebirthUpdater:FireServer()
Cash.Value = Cash.Value - RebirthPrice.Value
CashUiUpdater()
Rebirths.Value = Rebirths.Value + 1
RebirthUiUpdater()
Increaser.Value = Increaser.Value + 1
IncreaserUiUpdater()
RebirthPrice.Value = math.floor(RebirthPrice.value * 2.5 / 1.2)
RebirthPriceUiUpdater()
print(Cash.Value .. " " .. Rebirths.Value .. " " .. RebirthPrice.Value .. " " .. Increaser.Value) else
print("Not enough money, missing :" .. RebirthPrice.Value - Cash.Value .. " Cash")
end
end)
Cash:GetPropertyChangedSignal("Value"):Connect(function() -- Remote event
end)
Sorry about the last script I did not comment it yet