Number variable keeps changing

I’ve made a number variable inside a scope so the variable is created multiple times but somehow it keeps changing after the second time the variable was made. The yellow text next to the “-” button displays the number variable. Here is the footage


Here is the script:

local module = {}

function module:RequestGemConvert()
	if script.Parent.GemConverter.Open.Value == false then
		script.Parent.GemConverter.Open.Value = true
		module:CloseAllGui("GemConverter")
		local Coins = 0
		local MaximumCoins = game.ReplicatedStorage.ConvertGems:InvokeServer(0,false) -- so we would only invoke it once
		
		script.Parent.GemConverter.Background:TweenPosition(UDim2.new(0.5,0,0.5,0),"Out","Quint",0.5,true)
		
		script.Parent.GemConverter.Background.Add.MouseButton1Click:Connect(function()
			Coins = Coins + 1			
		end)
		
		script.Parent.GemConverter.Background.Subtract.MouseButton1Click:Connect(function()
			Coins = Coins - 1
		end)
		
		script.Parent.GemConverter.Background.Max.MouseButton1Click:Connect(function()
			Coins = MaximumCoins
		end)
		
		script.Parent.GemConverter.Background.Min.MouseButton1Click:Connect(function()
			Coins = 0
		end)
		
		script.Parent.GemConverter.Background.Convert.MouseButton1Click:Connect(function()
			game.ReplicatedStorage.ConvertGems:InvokeServer(Coins,true)
			script.Parent.GemConverter.Background:TweenPosition(UDim2.new(0.5,0,1.5,0),"In","Quint",0.5,true)
			module:SendNotification("Awesome!","Successfully converted your coins to Gems!")
			script.Parent.GemConverter.Open.Value = false
		end)
		
		script.Parent.GemConverter.Background.Exit.MouseButton1Click:Connect(function()
			script.Parent.GemConverter.Background:TweenPosition(UDim2.new(0.5,0,1.5,0),"In","Quint",0.5,true)
			script.Parent.GemConverter.Open.Value = false
			Coins = 0
		end)
		
		
		while wait() do
			script.Parent.GemConverter.Background.Coins.TextLabel.Text = module:RequestPlaceValue(Coins)
			script.Parent.GemConverter.Background.Gems.TextLabel.Text = module:RequestPlaceValue(math.floor(Coins / 5))
			if Coins == game.Players.LocalPlayer:WaitForChild("Leaderstats").Coins.Value then
				script.Parent.GemConverter.Background.Add.Visible = false
				script.Parent.GemConverter.Background.Subtract.Visible = true
			elseif Coins == 0 then
				script.Parent.GemConverter.Background.Subtract.Visible = false
				script.Parent.GemConverter.Background.Add.Visible = true
			else
				script.Parent.GemConverter.Background.Add.Visible = true
				script.Parent.GemConverter.Background.Subtract.Visible = true
			end
		end
		
		
	else
		script.Parent.GemConverter.Background:TweenPosition(UDim2.new(0.5,0,1.5,0),"In","Quint",0.5,true)
		script.Parent.GemConverter.Open.Value = false
	end

return module

Here is the server script:

game.ReplicatedStorage.ConvertGems.OnServerInvoke = function(Player,Coins,Convert)
	if Convert then
		Player:WaitForChild("Leaderstats").Gems.Value = Player:WaitForChild("Leaderstats").Gems.Value + math.floor(Coins / 5)
		Player:WaitForChild("Leaderstats").Coins.Value = Player:WaitForChild("Leaderstats").Coins.Value - Coins
	else
		return Player:WaitForChild("Leaderstats").Coins.Value
	end
end

Also, the RequestGemConvert function is fired when the player’s humanoidrootpart is near to the object I set it

The yellow text near the “-” button displays the number variable! Also the RequestGemConvert function is fired whenever the player’s HumanoidRootpart is near the object I set

How many times/when is this called? If called more than once, you have multiple loops going on. That may be an issue.

1 Like

Can you post the RequestPlaceValue module script, please?

It would be more helpful if you could show us all the scripts involved that makes this work.

The function is fired whenever the player is near the object I set it to. Here is the script that is responsible for firing the function

local UIS = game:GetService("UserInputService")

UIS.InputEnded:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.Keyboard or Input.UserInputType == Enum.UserInputType.Gamepad1 then
		if Input.KeyCode == Enum.KeyCode.E or Input.KeyCode == Enum.KeyCode.ButtonY then
			if UIS:GetFocusedTextBox() == nil then
				local Distance = (workspace.Lands.Forest.GemMachine.PrimaryPart.Position-game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
				if Distance <= 10 then
					local Module = require(script.Parent.Parent.Parent.Parent.MainModule)
					Module:RequestGemConvert()
				end
			end
		end
	end
end)

script.Parent.Display.MouseButton1Click:Connect(function()
	local Module = require(script.Parent.Parent.Parent.Parent.MainModule)
	Module:RequestGemConvert()
end)

while wait() do
	if script.Parent.Display.Size == UDim2.new(1,0,1,0) then script.Parent.Display:TweenSize(UDim2.new(0,0,0,0),"In","Back",0.3,true) end
	local Distance = (workspace.Lands.Forest.GemMachine.PrimaryPart.Position-game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
	if Distance <= 10 then
		script.Parent.Display:TweenSize(UDim2.new(1,0,1,0),"Out","Quint",0.5,true)
	end
end

Can you post the RequestPlaceValue module script, please???

Ok. Here is the script that fires the function from the module script

local UIS = game:GetService("UserInputService")

UIS.InputEnded:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.Keyboard or Input.UserInputType == Enum.UserInputType.Gamepad1 then
		if Input.KeyCode == Enum.KeyCode.E or Input.KeyCode == Enum.KeyCode.ButtonY then
			if UIS:GetFocusedTextBox() == nil then
				local Distance = (workspace.Lands.Forest.GemMachine.PrimaryPart.Position-game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
				if Distance <= 10 then
					local Module = require(script.Parent.Parent.Parent.Parent.MainModule)
					Module:RequestGemConvert()
				end
			end
		end
	end
end)

script.Parent.Display.MouseButton1Click:Connect(function()
	local Module = require(script.Parent.Parent.Parent.Parent.MainModule)
	Module:RequestGemConvert()
end)

while wait() do
	if script.Parent.Display.Size == UDim2.new(1,0,1,0) then script.Parent.Display:TweenSize(UDim2.new(0,0,0,0),"In","Back",0.3,true) end
	local Distance = (workspace.Lands.Forest.GemMachine.PrimaryPart.Position-game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
	if Distance <= 10 then
		script.Parent.Display:TweenSize(UDim2.new(1,0,1,0),"Out","Quint",0.5,true)
	end
end

here’s the module script that contains the function

function module:RequestGemConvert()
	if script.Parent.GemConverter.Open.Value == false then
		script.Parent.GemConverter.Open.Value = true
		module:CloseAllGui("GemConverter")
		local Coins = 0
		local MaximumCoins = game.ReplicatedStorage.ConvertGems:InvokeServer(0,false) -- so we would only invoke it once
		
		script.Parent.GemConverter.Background:TweenPosition(UDim2.new(0.5,0,0.5,0),"Out","Quint",0.5,true)
		
		script.Parent.GemConverter.Background.Add.MouseButton1Click:Connect(function()
			Coins = Coins + 1			
		end)
		
		script.Parent.GemConverter.Background.Subtract.MouseButton1Click:Connect(function()
			Coins = Coins - 1
		end)
		
		script.Parent.GemConverter.Background.Max.MouseButton1Click:Connect(function()
			Coins = MaximumCoins
		end)
		
		script.Parent.GemConverter.Background.Min.MouseButton1Click:Connect(function()
			Coins = 0
		end)
		
		script.Parent.GemConverter.Background.Convert.MouseButton1Click:Connect(function()
			game.ReplicatedStorage.ConvertGems:InvokeServer(Coins,true)
			script.Parent.GemConverter.Background:TweenPosition(UDim2.new(0.5,0,1.5,0),"In","Quint",0.5,true)
			module:SendNotification("Awesome!","Successfully converted your coins to Gems!")
			script.Parent.GemConverter.Open.Value = false
		end)
		
		script.Parent.GemConverter.Background.Exit.MouseButton1Click:Connect(function()
			script.Parent.GemConverter.Background:TweenPosition(UDim2.new(0.5,0,1.5,0),"In","Quint",0.5,true)
			script.Parent.GemConverter.Open.Value = false
			Coins = 0
		end)
		
		
		while wait() do
			script.Parent.GemConverter.Background.Coins.TextLabel.Text = module:RequestPlaceValue(Coins)
			script.Parent.GemConverter.Background.Gems.TextLabel.Text = module:RequestPlaceValue(math.floor(Coins / 5))
			if Coins == game.Players.LocalPlayer:WaitForChild("Leaderstats").Coins.Value then
				script.Parent.GemConverter.Background.Add.Visible = false
				script.Parent.GemConverter.Background.Subtract.Visible = true
			elseif Coins == 0 then
				script.Parent.GemConverter.Background.Subtract.Visible = false
				script.Parent.GemConverter.Background.Add.Visible = true
			else
				script.Parent.GemConverter.Background.Add.Visible = true
				script.Parent.GemConverter.Background.Subtract.Visible = true
			end
		end
		
		
	else
		script.Parent.GemConverter.Background:TweenPosition(UDim2.new(0.5,0,1.5,0),"In","Quint",0.5,true)
		script.Parent.GemConverter.Open.Value = false
	end

Here’s the script that receives the call from the modulescript that contains the function

game.ReplicatedStorage.ConvertGems.OnServerInvoke = function(Player,Coins,Convert)
	if Convert then
		Player:WaitForChild("Leaderstats").Gems.Value = Player:WaitForChild("Leaderstats").Gems.Value + math.floor(Coins / 5)
		Player:WaitForChild("Leaderstats").Coins.Value = Player:WaitForChild("Leaderstats").Coins.Value - Coins
	else
		return Player:WaitForChild("Leaderstats").Coins.Value
	end
end

Yeah, the issue stems from you calling RequestGemConvert multiple times. You should only be running that while loop once. You can notice this as I see your UI functions normally the first time you hit ‘E’, and it bugs out the 2nd, and so-forth times.

You also connect a ton of connections calling that function, which will lead to a memory leak.

You should only be running this function once, from the looks of it. Have another UI toggle whether the conversion UI is open or not.

1 Like

Why did you delete? I was interested in seeing the reply lol.

I accidentaly replied 2 times with the same post so i deleted 1

Thanks for telling me the issue! Now the script will check if the value of the boolvalue that I made is false then it would call the function if it returns true and it sure did got rid of the bug! Also thanks for informing me ab memory leaks. I usually dont care ab client memory and stuff but now im concerned ab those things!

1 Like