I have a coins system (made in leaderstats). I want to give coins based on player count. The problem is that I don’t really know how to do this.
If there are less players, the players get more coins but if the player count is higher then the players are gonna get less coins.
Max player count = 8
For example is the player count is 1 then player is going to get like 5._ coins but if the player count is 8 then players are going to get like 1._ coins.
You can try to get every element of the players with a four loop and in the for loop you can increase a counter value after each element has been detected then you will know what is the current players number and then you can set for each amount of players to get x amount of money.
I know how to check how many players are there in the server but i want to give more coins if the player count is small and less coins if the player count is high
its really relative depending on how much is high for you and how much is low for you? And you want fixed coin drops or you want it to be calculated automatically which can result in dynamic coin drops instead of fixed ones
I believe you may be looking for the following code:
-- Added reward addition +
-- Include this script
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TimeStats")
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local Coins = Instance.new("IntValue") -- Create a Coins stat
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = Leaderstats
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Coins.Value = Data
end
coroutine.resume(coroutine.create(function()
while true do
wait(60) -- Adjust this time interval as needed
Coins.Value = Coins.Value + 1 -- Increment the Coins value
end
end))
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, Player.leaderstats.Coins.Value)
end)
-- Define coin rewards based on player count
local smallPlayerCountReward = 5.0 -- Adjust these values as needed
local largePlayerCountReward = 1.0
local maxPlayerCount = 8
-- Connect the script to player added event
game.Players.PlayerAdded:Connect(function(player)
local stats = player:WaitForChild("leaderstats")
local coins = stats:FindFirstChild("Coins")
if coins then
local playerCount = #game.Players:GetPlayers()
local reward
if playerCount <= maxPlayerCount then
reward = smallPlayerCountReward
else
reward = largePlayerCountReward
end
coins.Value = reward
end
end)