Upgrading Stats doesn't work as expected

I need help making a Upgrading System, I can’t figure out the issue so I am creating this post.

The issue is that the upgrades don’t work how they should, it should upgrade once every time u click it. But if there are multiple players in the server it upgrades the amount of players in the server so if there are 2 ppl in the server it upgrades twice and so on.

Client:

-- Variables

local plr = game:GetService("Players").LocalPlayer

-- Dictionaries

local Settings = {
	
	MultiplierCap = 100,
	GemMultiplierCap = 6
}

local PlrStats = {
	
	MultiLevel = plr:WaitForChild("ValueFolder").MultiLevel,
	Multiplier = plr:WaitForChild("ValueFolder").Multiplier,
	GemsLevel = plr:WaitForChild("ValueFolder").GemsLevel,
	GemsMulti = plr:WaitForChild("ValueFolder").GemsMulti,
	Gemstones = plr:WaitForChild("ValueFolder").Gemstones
	
}

local abbreviationss = {
	"";
	"k";
	"m";
	"b";
	"t";
	"qd";
	"qi";
	"sx";
	"sp";
	"oc";
	"no";
	"dc";
	"ud";
	"dd";
	"dD";
	"td";
	"qad";
	"qid";
	"sxd";
	"spd";
	"ocd";
	"nod";
	"vg";
	"uvg"
}

local function abbreviate(val)
	for i=1, #abbreviationss do
		if tonumber(val) < 10^(i*3) then
			return math.floor(val/((10^((i-1)*3))/100))/(100)..abbreviationss[i]
		end
	end
end

local PriceTable = {

	12256, 14946, 25765, 28102, 30870, 44479, 44748, 50453, 59182, 60426, 63687, 68221, 69601, 69612, 70874, 73336, 75221, 79453, 91221, 98975, 99077, 102124, 104548, 114134, 114852, 120241, 125774, 127510, 139021, 169855, 170837, 176186, 176434, 177141, 178047, 178511, 182363, 183198, 184250, 189043, 194301, 198281, 204587, 206860, 214620, 219217, 219427, 221891, 221983, 222585, 226079, 235104, 240792, 245078, 246042, 248252, 261605, 262675, 266908, 268532, 270882, 284817, 288086, 288453, 293858, 294903, 298210, 304636, 308172, 308805, 312159, 313760, 313931, 317052, 317742, 318004, 328754, 328846, 356518, 367453, 371453, 375908, 378131, 391316, 397795, 406666, 415867, 420892, 427415, 427659, 430038, 431287, 437559, 450809, 460784, 469912, 476854, 484316, 487275

}

local GemsTable = {


}

-- Code

repeat wait() until plr:FindFirstChild("ValueFolder")

script.Parent.Clicks.confirmBtn.MouseButton1Click:Connect(function()

	if PlrStats.MultiLevel.Value < Settings.MultiplierCap then

		game:GetService("ReplicatedStorage"):WaitForChild("Communication"):WaitForChild("Events"):WaitForChild("Upgrade"):FireServer("ClickMulti")

	else

		local template = plr.PlayerGui.RewardInterface.List.UIListLayout.Template:Clone()

		template.Parent =  plr.PlayerGui.RewardInterface.List
		template.Text = "you cant upgrade to more than x".. Settings.MultiplierCap .."!"
		template.TextColor3 = Color3.fromRGB(255, 43, 46)
		game:GetService("TweenService"):Create(script.Parent.Multi.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(255, 43, 46)}):Play()
		wait(.6)
		game:GetService("TweenService"):Create(script.Parent.Multi.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(85, 170, 255)}):Play()

	end

end)

script.Parent.Gems.confirmBtn.MouseButton1Click:Connect(function()

	if PlrStats.GemsLevel.Value < Settings.GemMultiplierCap then

		game:GetService("ReplicatedStorage"):WaitForChild("Communication"):WaitForChild("Events"):WaitForChild("Upgrade"):FireServer("GemsMulti")

	else

		local template = plr.PlayerGui.RewardInterface.List.UIListLayout.Template:Clone()

		template.Parent =  plr.PlayerGui.RewardInterface.List
		template.Text = "you cant upgrade to more than x".. Settings.GemMultiplierCap .."!"
		template.TextColor3 = Color3.fromRGB(255, 43, 46)
		game:GetService("TweenService"):Create(script.Parent.Gems.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(255, 43, 46)}):Play()
		wait(.6)
		game:GetService("TweenService"):Create(script.Parent.Gems.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(61, 236, 134)}):Play()



	end

end)

local function Update()
	
	script.Parent:WaitForChild("Clicks").Frame.Level.Text = "Lvl.".. PlrStats.MultiLevel.Value
	script.Parent:WaitForChild("Gems").Frame.Level.Text = "Lvl.".. PlrStats.GemsLevel.Value	
	
	script.Parent:WaitForChild("Clicks").Frame.Count.Text = abbreviate(PriceTable[PlrStats.MultiLevel.Value])
	script.Parent:WaitForChild("Gems").Frame.Count.Text = abbreviate(PriceTable[PlrStats.GemsLevel.Value])
	
	script.Parent:WaitForChild("Gems").Frame.BarBG.BarLine.Size = UDim2.new(PlrStats.GemsLevel.Value/Settings.GemMultiplierCap, 0,1, 0)
	script.Parent:WaitForChild("Clicks").Frame.BarBG.BarLine.Size = UDim2.new(PlrStats.MultiLevel.Value/Settings.MultiplierCap, 0,1, 0)
	
end

spawn(Update)

plr.ValueFolder.GemsLevel:GetPropertyChangedSignal("Value"):Connect(function()
	
	script.Parent.Gems.Frame.BarBG.BarLine.Size = UDim2.new(PlrStats.GemsLevel.Value/Settings.GemMultiplierCap, 0,1, 0)

	local template = plr.PlayerGui.RewardInterface.List.UIListLayout.Template:Clone()

	template.Parent =  plr.PlayerGui.RewardInterface.List
	template.Text = "successfully upgraded Clicks to x".. PlrStats.MultiLevel.Value .."!"
	template.TextColor3 = Color3.fromRGB(0, 255, 127)
	
	script.Parent.Gems.Frame.Level.Text = "Lvl.".. PlrStats.GemsLevel.Value
	
	script.Parent:WaitForChild("Gems").Frame.Count.Text = abbreviate(PriceTable[PlrStats.GemsLevel.Value])
	
	game:GetService("TweenService"):Create(script.Parent.Gems.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(0, 255, 127)}):Play()
	wait(.6)
	game:GetService("TweenService"):Create(script.Parent.Gems.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(85, 170, 255)}):Play()
	
end)
PlrStats.MultiLevel:GetPropertyChangedSignal("Value"):Connect(function()
	
	script.Parent.Clicks.Frame.BarBG.BarLine.Size = UDim2.new(PlrStats.MultiLevel.Value/Settings.MultiplierCap, 0,1, 0)
	
	local template = plr.PlayerGui.RewardInterface.List.UIListLayout.Template:Clone()

	template.Parent =  plr.PlayerGui.RewardInterface.List
	template.Text = "successfully upgraded Clicks to x".. PlrStats.MultiLevel.Value .."!"
	template.TextColor3 = Color3.fromRGB(0, 255, 127)
	
	script.Parent.Clicks.Frame.Level.Text = "Lvl.".. PlrStats.MultiLevel.Value
	
	script.Parent:WaitForChild("Clicks").Frame.Count.Text = abbreviate(PriceTable[PlrStats.MultiLevel.Value])
	
	game:GetService("TweenService"):Create(script.Parent.Clicks.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(0, 255, 127)}):Play()
	wait(.6)
	game:GetService("TweenService"):Create(script.Parent.Clicks.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(85, 170, 255)}):Play()
	
end)

Server:

local MaxClick = 100
local MaxGem = 6

local PriceTable = {

	12256, 14946, 25765, 28102, 30870, 44479, 44748, 50453, 59182, 60426, 63687, 68221, 69601, 69612, 70874, 73336, 75221, 79453, 91221, 98975, 99077, 102124, 104548, 114134, 114852, 120241, 125774, 127510, 139021, 169855, 170837, 176186, 176434, 177141, 178047, 178511, 182363, 183198, 184250, 189043, 194301, 198281, 204587, 206860, 214620, 219217, 219427, 221891, 221983, 222585, 226079, 235104, 240792, 245078, 246042, 248252, 261605, 262675, 266908, 268532, 270882, 284817, 288086, 288453, 293858, 294903, 298210, 304636, 308172, 308805, 312159, 313760, 313931, 317052, 317742, 318004, 328754, 328846, 356518, 367453, 371453, 375908, 378131, 391316, 397795, 406666, 415867, 420892, 427415, 427659, 430038, 431287, 437559, 450809, 460784, 469912, 476854, 484316, 487275


}

local GemsTable = {


}

game:GetService("ReplicatedStorage"):WaitForChild("Communication"):WaitForChild("Events"):WaitForChild("Upgrade").OnServerEvent:Connect(function(plr, typ)

	if typ == "ClickMulti" then

		if plr.ValueFolder.Gemstones.Value >= tonumber(PriceTable[plr.ValueFolder.MultiLevel.Value]) then

			if plr.ValueFolder.Multiplier.Value <= MaxClick then

				plr.ValueFolder.Gemstones.Value -= PriceTable[plr.ValueFolder.MultiLevel.Value]
				
				if plr.ValueFolder.MultiLevel.Value ~= MaxClick then
					plr.ValueFolder.MultiLevel.Value += 1
				end

				if plr.ValueFolder.Multiplier.Value ~= MaxClick then
					plr.ValueFolder.Multiplier.Value += 1
				end
				return

			end

		end

	elseif typ == "GemsMulti" then

		if plr.ValueFolder.GemsLevel.Value == 0 then

			if plr.ValueFolder.Gemstones.Value >= 500 then

				plr.ValueFolder.Gemstones.Value -= 500
				plr.ValueFolder.GemsLevel.Value += 1

			end
			return

		end

		if plr.ValueFolder.Gemstones.Value >= tonumber(PriceTable[plr.ValueFolder.GemsLevel.Value]) then

			if plr.ValueFolder.GemsMulti.Value <= MaxGem then

				plr.ValueFolder.Gemstones.Value -= PriceTable[plr.ValueFolder.GemsLevel.Value]
				plr.ValueFolder.GemsLevel.Value += 1

				if plr.ValueFolder.GemsMulti.Value ~= MaxGem then
					plr.ValueFolder.GemsMulti.Value += 1
				end

			end

		end


	end

end)

I have tried to find many solutions but couldn’t figure any out which could fix this issue.
Thanks for taking your time to help me!

Still didnt find any fix. Does anyone know how to get rid of this issue?

This has no replies because it’s very hard to help you. Questions I have, and probably others:

  • What are Gemstones in your game?
  • What about GemsLevel?
  • GemsMulti? Multiplier?
  • MaxGem? MaxClick?
  • Prices for what?
  • Why are you using WaitForChild on the server?

There’s no real way to mind read and know exactly what you’re trying to do. This could be a simulator game of any kind, a tycoon game of any kind, or a mining game. Which is it?

For now, I made the scripts easier to read, for my own sanity:

Client

local player = game.Player.LocalPlayer

local Settings = {
	MultiplierCap = 100,
	GemMultiplierCap = 6
}

local stats = player:WaitForChild("ValueFolder")
local upg = game.ReplicatedStorage:WaitForChild("Communcation"):WaitForChild("Events"):WaitForChild("Upgrade")
local ui = script.Parent

local abbreviations = {
	"";
	"k";
	"m";
	"b";
	"t";
	"qd";
	"qi";
	"sx";
	"sp";
	"oc";
	"no";
	"dc";
	"ud";
	"dd";
	"dD";
	"td";
	"qad";
	"qid";
	"sxd";
	"spd";
	"ocd";
	"nod";
	"vg";
	"uvg"
}

local function abbreviate(val)
	for i=1, #abbreviations do
		if tonumber(val) < 10^(i*3) then
			return math.floor(val/((10^((i-1)*3))/100))/(100)..abbreviations[i]
		end
	end
end

local PriceTable = {12256, 14946, 25765, 28102, 30870, 44479, 44748, 50453, 59182, 60426, 63687, 68221, 69601, 69612, 70874, 73336, 75221, 79453, 91221, 98975, 99077, 102124, 104548, 114134, 114852, 120241, 125774, 127510, 139021, 169855, 170837, 176186, 176434, 177141, 178047, 178511, 182363, 183198, 184250, 189043, 194301, 198281, 204587, 206860, 214620, 219217, 219427, 221891, 221983, 222585, 226079, 235104, 240792, 245078, 246042, 248252, 261605, 262675, 266908, 268532, 270882, 284817, 288086, 288453, 293858, 294903, 298210, 304636, 308172, 308805, 312159, 313760, 313931, 317052, 317742, 318004, 328754, 328846, 356518, 367453, 371453, 375908, 378131, 391316, 397795, 406666, 415867, 420892, 427415, 427659, 430038, 431287, 437559, 450809, 460784, 469912, 476854, 484316, 487275}
local GemsTable = {}

ui.Clicks.confirmBtn.MouseButton1Click:Connect(function()
	if stats.MultiLevel.Value < Settings.MultiplierCap then
		upg:FireServer("ClickMulti")
	else
		local template = player.PlayerGui.RewardInterface.List.UIListLayout.Template:Clone()

		template.Parent =  player.PlayerGui.RewardInterface.List
		template.Text = "you cant upgrade to more than x".. Settings.MultiplierCap .."!"
		template.TextColor3 = Color3.fromRGB(255, 43, 46)

		game.TweenService:Create(ui.Multi.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(255, 43, 46)}):Play()
		task.wait(0.6)
		game.TweenService:Create(ui.Multi.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(85, 170, 255)}):Play()

	end

end)

ui.Gems.confirmBtn.MouseButton1Click:Connect(function()
	if stats.GemsLevel.Value < Settings.GemMultiplierCap then
		upg:FireServer("GemsMulti")
	else
		local template = player.PlayerGui.RewardInterface.List.UIListLayout.Template:Clone()

		template.Parent =  player.PlayerGui.RewardInterface.List
		template.Text = "you cant upgrade to more than x".. Settings.GemMultiplierCap .."!"
		template.TextColor3 = Color3.fromRGB(255, 43, 46)
		game.TweenService:Create(ui.Gems.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(255, 43, 46)}):Play()
		task.wait(0.6)
		game.TweenService:Create(ui.Gems.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(61, 236, 134)}):Play()
	end
end)

local function Update()
	ui:WaitForChild("Clicks").Frame.Level.Text = "Lvl.".. stats.MultiLevel.Value
	ui:WaitForChild("Gems").Frame.Level.Text = "Lvl.".. stats.GemsLevel.Value	
	
	ui:WaitForChild("Clicks").Frame.Count.Text = abbreviate(PriceTable[stats.MultiLevel.Value])
	ui:WaitForChild("Gems").Frame.Count.Text = abbreviate(PriceTable[stats.GemsLevel.Value])
	
	ui:WaitForChild("Gems").Frame.BarBG.BarLine.Size = UDim2.new(stats.GemsLevel.Value/Settings.GemMultiplierCap, 0,1, 0)
	ui:WaitForChild("Clicks").Frame.BarBG.BarLine.Size = UDim2.new(stats.MultiLevel.Value/Settings.MultiplierCap, 0,1, 0)
end

task.spawn(Update)

player.ValueFolder.GemsLevel:GetPropertyChangedSignal("Value"):Connect(function()
	
	ui.Gems.Frame.BarBG.BarLine.Size = UDim2.new(stats.GemsLevel.Value/Settings.GemMultiplierCap, 0,1, 0)

	local template = player.PlayerGui.RewardInterface.List.UIListLayout.Template:Clone()

	template.Parent =  player.PlayerGui.RewardInterface.List
	template.Text = "successfully upgraded Clicks to x".. stats.MultiLevel.Value .."!"
	template.TextColor3 = Color3.fromRGB(0, 255, 127)
	
	ui.Gems.Frame.Level.Text = "Lvl.".. stats.GemsLevel.Value
	
	ui:WaitForChild("Gems").Frame.Count.Text = abbreviate(PriceTable[stats.GemsLevel.Value])
	
	game.TweenService:Create(ui.Gems.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(0, 255, 127)}):Play()
	task.wait(0.6)
	game.TweenService:Create(ui.Gems.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(85, 170, 255)}):Play()
	
end)

stats.MultiLevel:GetPropertyChangedSignal("Value"):Connect(function()
	ui.Clicks.Frame.BarBG.BarLine.Size = UDim2.new(stats.MultiLevel.Value/Settings.MultiplierCap, 0,1, 0)
	
	local template = player.PlayerGui.RewardInterface.List.UIListLayout.Template:Clone()

	template.Parent =  player.PlayerGui.RewardInterface.List
	template.Text = "successfully upgraded Clicks to x".. stats.MultiLevel.Value .."!"
	template.TextColor3 = Color3.fromRGB(0, 255, 127)
	
	ui.Clicks.Frame.Level.Text = "Lvl.".. stats.MultiLevel.Value
	
	ui:WaitForChild("Clicks").Frame.Count.Text = abbreviate(PriceTable[stats.MultiLevel.Value])
	
	game.TweenService:Create(ui.Clicks.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(0, 255, 127)}):Play()
	task.wait(0.6)
	game.TweenService:Create(ui.Clicks.confirmBtn, TweenInfo.new(0.5), {BackgroundColor3 = Color3.fromRGB(85, 170, 255)}):Play()
end)

Server

local MaxClick = 100
local MaxGem = 6

local PriceTable = {12256, 14946, 25765, 28102, 30870, 44479, 44748, 50453, 59182, 60426, 63687, 68221, 69601, 69612, 70874, 73336, 75221, 79453, 91221, 98975, 99077, 102124, 104548, 114134, 114852, 120241, 125774, 127510, 139021, 169855, 170837, 176186, 176434, 177141, 178047, 178511, 182363, 183198, 184250, 189043, 194301, 198281, 204587, 206860, 214620, 219217, 219427, 221891, 221983, 222585, 226079, 235104, 240792, 245078, 246042, 248252, 261605, 262675, 266908, 268532, 270882, 284817, 288086, 288453, 293858, 294903, 298210, 304636, 308172, 308805, 312159, 313760, 313931, 317052, 317742, 318004, 328754, 328846, 356518, 367453, 371453, 375908, 378131, 391316, 397795, 406666, 415867, 420892, 427415, 427659, 430038, 431287, 437559, 450809, 460784, 469912, 476854, 484316, 487275}
-- please fix the price table... it's hardcoded

local GemsTable = {}

game.ReplicatedStorage.Communication.Events.Upgrade.OnServerEvent:Connect(function(plr, type)
	local stat = plr.ValueFolder

	if type == "ClickMulti" then
		if stat.Gemstones.Value >= PriceTable[stat.MultiLevel.Value] then
			if stat.Multiplier.Value <= MaxClick then
				stat.Gemstones.Value -= PriceTable[stat.MultiLevel.Value]
				
				if stat.MultiLevel.Value ~= MaxClick then
					stat.MultiLevel.Value += 1
				end

				if stat.Multiplier.Value ~= MaxClick then
					stat.Multiplier.Value += 1
				end
			end
		end
	elseif type == "GemsMulti" then
		if stat.GemsLevel.Value == 0 then
			if stat.Gemstones.Value >= 500 then
				stat.Gemstones.Value -= 500
				stat.GemsLevel.Value += 1
			end
		end

		if stat.Gemstones.Value >= PriceTable[stat.GemsLevel.Value] then
			if stat.GemsMulti.Value <= MaxGem then
				stat.Gemstones.Value -= PriceTable[stat.GemsLevel.Value]
				stat.GemsLevel.Value += 1

				if stat.GemsMulti.Value ~= MaxGem then
					stat.GemsMulti.Value += 1
				end
			end
		end
	end
end)

I strongly suggest making the Price list not hardcoded, and store it in a module or in upgrade instances containing all info about the upgrades.

1 Like

Hello and thanks for your reply, Gemstones is the currency used to purchase upgrades, GemsLevel is your Gems Upgrade Level, GemsMulti is your Gems Multiplier, Multiplier is your Click Multiplier, MaxGem is the Maximum amount that can be upgraded to for gem multiplier and MaxClick is the maximum amount that can be upgraded to for click multiplier. Prices are the cost of the upgrade and I was using waitforchild on the server & client for 2 years and I don’t see a problem with that. the game type is simulator;

I’ll also remake the price table soon.

1 Like

There isn’t really a problem, it’s just redundant. Most likely you don’t need to wait for anything to load in on the server. The only exception is that you’re waiting for a script of yours to add a child into something. Only the client has to sometimes wait for an instance to load in.

I suggest making a module script with abbreviation functions, since boilerplate code is never a good idea.

Something like this:

local Gem = {}

Gem.Suffix = {
	"", "k", "m", "b", "t", "qd",
	"qn", "sx", "sp", "o", "n", 
    "dc", "ud", "dd", "td", "qdD",
	"qnD", "sxD", "spD", "ocD",
	"noD", "vG", "uvG", "!", "!!"
}

function Gem:Abbreviate(x)
    for magnitude, suffix in pairs(Gem.Suffix) do
        if tonumber(x) < 10^(magnitude*3) then -- why *3 ?
            return tostring(math.floor(x/((10^((magnitude-1)*3))/100))/(100))..suffix
        end
    end
    return tostring(x):sub(1,3) .. "!!!"
end

return Gem
1 Like

Thank you. I’ll create a module for that!
I’ll also try the new script, if it still does not work im gonna rewrite the whole script.

1 Like

I did not fix it yet, I just beautified it. But you never know, maybe that fixed it!

1 Like

Yeah, I see it looks better now… I didn’t even know you can use “type” for that so i just removed the “e”.

That’s how it looks like with the bug (i spammed the upgrade button so you can see the +2 thing)

image

is that pls donate

So is the issue that your upgrading the amount of times equivalent to the amount of players, in one click, instead of once?

yes, exactly. and yea its a simulator version of pls donate

Can you confirm that it upgrades 3 times with 3 players, and 4 times with 4 players? It’s very hard to fix this script because I did not make it your game, and I don’t know what all these values are for.
I suggest using print and/or breakpoints to see how your script actually works.

i’ll try out debugging with print, i’ll reply to you once im finished.

Oh my god. I placed the script in a Gui in StarterGui so its there for every player. Im actually dumb as hell, thanks tho!
image

thats why i used *3
1 000
1 000 000
1 000 000 000

I don’t think you would multiply by 3. Maybe use somebody else’s cash abbreviation methods.

It’s working perfectly fine so I don’t see why I should use somebody else’s method

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.