0

# how to make a value for all players?

I need a value to count up something that's for all players, but I don't know how. For example Player1 clicks on a button and the value goes +1 (so now it's 1) Player 2 does the same now the value is 2.

I need it for a egg hatching system to count the amount of each pet that is hatched.

1

This can be done very easily by creating a single value and adding to the value every time an egg is hatched.

local EggsHatched = workspace.Values.EggsHatched -- Assuming the value "EggsHatched" is in workspace inside a folder called Values.

-- Run this code on the server below every time an egg is hatched.
EggsHatched += 1



If you want to read the value on the client put the NumberValue inside a place accessable to the client, such as ReplicatedStorage or Workspace.

When checking the clicks on the client you would need a remote event to communicate with the server about adding to the value. Read more about remote events here.

If you need this value to be saved i would suggest looking into the DataStoreService.

0
ok so I tested it quickly with PlayerAdded and added 1, then I started a test server with multiple players and the value was the amount of players in game. so this seems to work. now I only need to save it and make it do what it has to do :D Thank you jeboysisi 26 — 6d
1

You can add a number value and place it somewhere like Players, then add a number value in players when they join, and then when the number value in Players updates, you change all the players' number values to the value of the number in Players.

local Players = game:GetService("Players")
local NumberValue = Players:WaitForChild(YourNumberValue)

if Player:FindFirstChild(ValueName) then
Player:FindFirstChild(ValueName):Destroy()
end

local Value = Instance.new("NumberValue")
Value.Name = ValueName
Value.Value = NumberValue.Value
Value.Parent = Player

NumberValue:GetPropertyChanged:Connect(function()
Value.Value = NumberValue.Value
end)
end)

0
so if I make clicks and totalclicks (of all players) what is YourNumberValue and what is ValueName, I think ValueName would be the totalclicks but do I need to add a value to the player clicks which would be YourNumberValue? jeboysisi 26 — 6d
0
Oh i misunderstood what you were trying to do SEAN_YT213 104 — 6d
0
You put a numbervalue is whatever service you want to, like ReplicatedStorage. When a player clicks a button, fire an remote event and then use a server script to add one to that numbervalue SEAN_YT213 104 — 6d
0
Ok I think I figured that out, but the players dont really need the value in their stats, its just a global value. Can I just put a value in players and add it +1 when the player hatches that pet or isn't that possible? jeboysisi 26 — 6d
0
Yeah that's what you should do, i just misunderstood the question you asked. SEAN_YT213 104 — 6d
0
will it automaticly remeber the value? like 5 players hatch that pet so the value is 5 but when they rejoin all it should still be 5 jeboysisi 26 — 6d
0
I won't SEAN_YT213 104 — 6d
0
It won't* SEAN_YT213 104 — 6d
0
Try using datastores to save the value SEAN_YT213 104 — 6d
0
I made a post clarifying it a little more, with links to the docs. peter21340 41 — 6d
0
Ok thank you, I wanted to accept your answer and the answer of the other guy because they both helped me to reach something that works but for some reason I cant accept 2 answers. jeboysisi 26 — 6d