Why is My Script Only focusing On 1 Player?

  1. I need my script to focus on all players

  2. If you Have Any Idea What The Game “Button Simulator” is Then, I made a game Like button simulator and The Stats Automatically Are suppose to increase by value, but the stats only automatically increase on one player and not other people. I used “PlayerAdded” Function.

  3. I tried Changing the script. But nothing worked

Location Of Script: ServerScriptService
Type: Normal Script
Here is the code:

Script

– So first we will be making the stats…

local Data = game:GetService(“DataStoreService”):GetDataStore(“dstore9”) – type something random in there
– And check if player has joined the game
game.Players.PlayerAdded:Connect(function(plr) – basically redirects you to the player here
local s = Instance.new(“Folder”,plr)
s.Name = “Stats” – it will show ur cash n stuff in the leaderboard
– change it to stats if u want the cash to show on a gui (will show later how to make.)
local cash = Instance.new(“NumberValue”,s) – cash stat
cash.Name = “Cash”
cash.Value = 0
local multi = Instance.new(“NumberValue”,s) – multi stat
multi.Name = “Multiplier”
multi.Value = 10
local reb = Instance.new(“NumberValue”,s) – rebirth stat
reb.Name = “Rebirth”
reb.Value = 1

local ult = Instance.new("NumberValue",s) -- ultra rebirth stat
ult.Name = "UltraRebirth"
ult.Value = 1 -- you can just add more and more stats

local pre = Instance.new("NumberValue",s) -- prestige stat
pre.Name = "Prestige"
pre.Value = 1

local dataload = Data:GetAsync(tostring(plr.UserId))
-- loading stats part
if dataload then
	cash.Value = dataload[1] or 0
	multi.Value = dataload[2] or 1
	reb.Value = dataload[3] or 1
	ult.Value = dataload[4] or 1 -- add it to the loading part
	pre.Value = dataload[5] or 1
end

while wait() do -- now the cash giver part
	if reb.Value >= 2 then
		multi.Value = multi.Value + (0.3*reb.Value)
	    cash.Value = cash.Value + (100*multi.Value)	

if ult.Value >= 2 then
while wait() do
multi.Value = multi.Value + (0.3reb.Value)
cash.Value = cash.Value + (100
multi.Value)
reb.Value = reb.Value + (0.3ult.Value)
if pre.Value >= 2 then
while wait() do
ult.Value = ult.Value + (0.3
pre.Value)
multi.Value = multi.Value + (0.3reb.Value)
cash.Value = cash.Value + (100
multi.Value)
reb.Value = reb.Value + (0.3*ult.Value)
end
end
end
end
end
end
end)

game.Players.PlayerRemoving:Connect(function(plr)
Data:SetAsync(tostring(plr.UserId),{
plr.Stats.Cash.Value, – if you use leaderstats then do plr.leaderstats.Cash.Value, same with the others
plr.Stats.Multiplier.Value,
plr.Stats.Rebirth.Value,
plr.Stats.UltraRebirth.Value, – adding it to save ofc
plr.Stats.Prestige.Value
})
end)

  • Script ↑
    If anyone has an Idea Of What I’m talking about and knows how to fix it, then please reply with your solution.

Thank you for you help!

Can we see the code and where its located in and what type of script it is?

I edited The Post. Now you can look at the code.

I dont really know what you mean by “For all players” please explain more.

When I join my game My Stats Value Automatically increases. But when Other Players Join, their stats Don’t Automatically Increase. (My script doesn’t read that other people joined after me, So it doesn’t automatically Give other players stats)

I am not sure but I think it is because the while loop is located IN the PlayerAdded function write it under the PlayerAdded function

The script seems to be doing its job in trying to update the player’s own data. Are you trying to say that you have a leaderboard GUI and that each player’s data isn’t updating on the GUI accordingly?

I doubt this will help since the variables are all declared in the function that’s connected to PlayerAdded. Placing it outside of the function will probably result in errors because the loop will process variables that don’t exist in the first place.

Yeah you need to define them… using for i,player in pairs(game.Players:GetPlayers()) do loop but maybe you can try printing words in the script of the GUI and see if it prints for everyone that word if it doesn’t then there is something wrong with the gui script as @LDHYA34750 said

That is actually false, as he’s connecting it to the PlayerAdded event. Each time an individual joins it will execute that function and the while shouldn’t be causing any sort of yield as it’s the last portion executed.

Side note, if the code was in a more readable fashion it would be easier to find unintended functionality.

Also, I’d like to point out that all your while loops are nested so the inner while loop will cause the outer one to yield. meaning only the inner most loop will begin to work once that if statement is hit. I don’t know if this was intended, but you may want to look into different ways of doing so.

And one more thing, they only do anything if you have rebirth set to 2, and your code defaults them to 1.

It would be much more preferable if you posted your related code to the thread over an image so that we can actually better see the code and interact with it (copy parts). A screenshot makes it difficult to review code. You can change the image for a code block (three back ticks above and below your code).

On that note, while this is not related, an instant recommendation I want to make is to stop using the second argument of Instance.new.

2 Likes
local Data = game:GetService("DataStoreService"):GetDataStore("dstore8")
local Players = game:GetService("Players")
local Connections = {}
local SpeedOfUpdate = 1; -- Time in seconds between value updates.

local function PlayerAdded(Player)
	local Success, Stats, Error = pcall(function() return Data:GetAsync(tostring(Player.UserId)) end)
	if not Success and Error then
		warn(Error)
	else
		local StatFolder = Instance.new("Folder")
		StatFolder.Name = "Stats-"..Player.UserId
		local Cash = Instance.new("NumberValue")
		Cash.Name = "Cash"
		local Multiplier = Instance.new("NumberValue")
		Multiplier.Name = "Multiplier"
		local Rebirth = Instance.new("NumberValue")
		Rebirth.Name = "Rebirth"
		local UltraRebirth = Instance.new("NumberValue")
		UltraRebirth.Name = "UltraRebirth"
		local Prestige = Instance.new("NumberValue")
		Prestige.Name = "Prestige"
		
		Cash.Value = Stats and Stats[1] or 0
		Multiplier.Value = Stats and Stats[2] or 10
		Rebirth.Value = Stats and Stats[3] or 1
		UltraRebirth.Value = Stats and Stats[4] or 1
		Prestige.Value = Stats and Stats[5] or 1
		
		local LastUpdate = tick() - SpeedOfUpdate
		
		Connections[Player] = game:GetService("RunService").Heartbeat:Connect(function()
			if tick() - LastUpdate >= SpeedOfUpdate then
				LastUpdate = tick()
				Cash.Value = Cash.Value + (100 * Multiplier.Value)
				Multiplier.Value = Multiplier.Value + (0.3 * Rebirth.Value)
				if UltraRebirth.Value >= 2 then
					Rebirth.Value = Rebirth.Value * (0.3 * UltraRebirth.Value)
				end
				if Prestige.Value >= 2 then
					UltraRebirth.Value = UltraRebirth.Value * (0.3 * Prestige.Value)
				end
				LastUpdate = tick()	
			end
		end)
		
		Cash.Parent = StatFolder
		Multiplier.Parent = StatFolder
		Rebirth.Parent = StatFolder
		UltraRebirth.Parent = StatFolder
		Prestige.Parent = StatFolder
		StatFolder.Parent = game.ReplicatedStorage
	end
end

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(Player)
	if Connections[Player] then Connections[Player]:Disconnect() end
	local Folder = game.ReplicatedStorage:FindFirstChild("Stats-"..Player.UserId)
	local Success, Error = pcall(function() 
		Data:SetAsync(tostring(Player.UserId),{
			Folder.Cash.Value,
			Folder.Multiplier.Value,
			Folder.Rebirth.Value,
			Folder.UltraRebirth.Value,
			Folder.Prestige.Value,
		})
	end)
	if not Success then
		warn(Error)
	else
		Folder:Destroy()
	end
end)

game:BindToClose(function()
	wait(1)
end)

note this isn’t a sure fire thing, and I moved the Stats Folder to ReplicatedStorage as from my experience the player gets garbage collected before the stats can actually save. The BindToClose was used for saving in studio.

I also went ahead and checked multiple user update in multi test mode.
https://gyazo.com/f1cb7e32a0efc1d657cf53f63f9a2e4f

1 Like