RNG game bug with equipping (TextButton)

What i want to achieve is very simple, fix the bug thats going on with the “equipButton.MouseButton1Click” function

I’ve tried adding stuff like “Once” at the end of the function instead of “Connect” but it doesnt work, also i should add that this happens after a while of rolling, i also believe that the error happens in the first “if” statement, at the beggining of the function

This is the script for the equipButton, if im asked to, ill show the full script

equipButton.MouseButton1Click:Once(function()
		if invFrame.Parent.Storage.Auras.Value ~= invFrame.Parent.Storage.Max.Value and equippedAnAura == false then
			equippedAnAura = true
			
			print("Equip: " .. tostring(result))
			equipEvent:FireServer(result)
			
			invFrame.Parent.Storage.Auras.Value += 1
			
			-- create a button
			local btn = exampleBtn:Clone()
			btn.Parent = invFrame
			btn.Visible = true
			btn.Text = tostring(result)
			btn.TextColor3 = color
			btn.UIStroke.Color = color
			btn.FontFace = font
			btn.Chance.Value = chance
			
			local auraBtn : TextButton = collectionFrame[tostring(result)]
			auraBtn.Locked.Value = false
			auraBtn.Text = tostring(result)
			auraBtn.TextColor3 = color
			auraBtn.FontFace = font
			auraBtn.Interactable = true
			
			print("Weighted Result: " .. result)

			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			blackFrame.Warning.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false
			
			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		elseif invFrame.Parent.Storage.Auras.Value == invFrame.Parent.Storage.Max.Value and equipWarned == false then
			equipWarned = true
		elseif equipWarned == true then
			blackFrame.Warning.Visible = true
			blackFrame.Warning.Text = "Not enough storage! (if you click equip, a random aura from your inventory will get deleted)"
			showedEquipWarning = true
		elseif invFrame.Parent.Storage.Auras.Value == invFrame.Parent.Storage.Max.Value and showedEquipWarning == true then
			for _, child in pairs(invFrame:GetChildren()) do
				if child:IsA("TextButton") then
					child:Destroy()
					invFrame.Parent.Storage.Auras.Value -= 1
					break
				end
			end
			
			print("Equip: " .. tostring(result))
			equipEvent:FireServer(result)
			
			-- create a button
			local btn = exampleBtn:Clone()
			btn.Parent = invFrame
			btn.Visible = true
			btn.Text = tostring(result)
			btn.TextColor3 = color
			btn.UIStroke.Color = color
			btn.FontFace = font
			btn.Chance.Value = chance
			
			local auraBtn : TextButton = collectionFrame[tostring(result)]
			auraBtn.Locked.Value = false
			auraBtn.Text = tostring(result)
			auraBtn.TextColor3 = color
			auraBtn.FontFace = font
			auraBtn.Interactable = true
			
			print("Weighted Result: " .. result)

			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false
			
			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		end
	end)

this part of the localscript is very simple, just equip an aura and create a button for it

Did you try and use a debug statement?

equipButton.MouseButton1Click:Connect(function()
 print("Clicked")
end)

If not could you try this, your code looks correct.

Yeah, i tried thats why theres a “Equip: result” next to the remote that equips the aura onto the player

Okay, that’s good I’m assuming it prints. Could the issue you are encountering is related to the player’s aura not equipping? Could you please be more descriptive besides MouseButton1Click not working?

I just figured something out after testing a while, the error happens when you skip an aura with the skip button.

here’s the skip code (also some variables that will make it have more sense):

local equippedAnAura = false
	local equipWarned = false
	local showedEquipWarning = false
	
	skipButton.MouseButton1Click:Once(function()
		local warned = false
		if chance > specialChance then
			blackFrame.Warning.Visible = true
			warned = true
		elseif warned == true then
			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false

			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		else
			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false
			
			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		end
	end)

in response to you question about if anything else doesnt work, no, everything works exactly as i want it to.

Hmmm okay. Firstly, if you have not did this yet, switch from Once to Connect for both of your MouseButton1Clicks. Secondly all your code seems fine, there shouldn’t be an issue with any of your MouseButton1Click functions as long as they are with Connect.

switching from Once to Connect didnt fix anything, sorry

Use more in depth debug statements

equipButton.MouseButton1Click:Connect(function()
    print("Auras Value: " .. tostring(invFrame.Parent.Storage.Auras.Value))
    print("Max Value: " .. tostring(invFrame.Parent.Storage.Max.Value))
    print("equippedAnAura: " .. tostring(equippedAnAura))
    print("equipWarned: " .. tostring(equipWarned))
    print("showedEquipWarning: " .. tostring(showedEquipWarning))
    -- rest of your code
end)

Looking at it now, It looks like you handling your values on the client, this can create serious security risks and could you make sure you are updating this value on the server as well invFrame.Parent.Storage.Auras.Value? Then remove it from the client as the server will replicate it to the client.

ill use more in depth debug statements, you want me to update the aruasvalue (intvalue) on the server aswell? (its inside of a gui)

Wait shouldn’t it have two if statements

Yes update any values that you are doing on the client on the server then remove them from the client. It’s good to store these values on the server because it adds an extra layer of security not a strong one but better than giving the client blown out access to manipulate it easily.

If you want to make it easy you could pass through the instance with the remote event just for testing then refactor it later.

local data = {
 Auras = invFrame.Parent.Storage.Auras,
 MaxValue = invFrame.Parent.Storage.Max
}

remoteEvent:FireServer(result, data)

This is a quick way of doing it

what do you mean (what would the second if statement have), ive already done every possibility i need to warn and equip an aura

Try this:


local equippedAnAura = false
	local equipWarned = false
	local showedEquipWarning = false
	
	skipButton.MouseButton1Click:Once(function()
		local warned = false
		if chance > specialChance then
			blackFrame.Warning.Visible = true
			warned = true
                end
		if warned == true then
			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false

			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		else
			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false
			
			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		end
	end)

`

How come the code is also copied… if warned then CODE else SAME CODE end? Just a question they both do the same thing if warned it true or false it does the same exact code.

they do the same thing to warn the player so if they click again, its because they didnt want that aura, even though it warned them:

  • player clicks the button
  • if the aura has a big enough chance it shows the warning
  • if the player clicks again then the aura gets skipped, if they click the equip button insted, it equips the aura

changing the values to the server doesnt change anything, but its more secure now atleast, ive also tried looking at more debugging values, its all working good

having another if statement also doesnt work, sorry

Okay so just to be clear after a while of playing your game your equipButton stops working randomly, but continues to print the values, This means that the connection is still tied which is good. Now we know one of the if statements are not being met. Could you figure out which if statement is not being met so we can narrow down this issue more?

no, its when an aura is skipped, as i told you before, then it stops working.
As for the if statement, i think its the first one, the one that has the “Equip:” result shows twice in the output

ill show you:

  • first i rolled a common
  • i skipped this common
  • then i rolled an uncommon
  • i equipped this uncommon
  • the auras that got inside of my inventory where both uncommon and common in that order:
    (when you skip an aura, that aura doesnt get printed into the output)

image

this is the full script

local rp = game:GetService("ReplicatedStorage")
local ss = game:GetService("SoundService")
local equipEvent = rp.EquipAura
local rollEvent = rp.Roll
local mainEvent = rp.MainEvent
local button = script.Parent
local Auras = require(rp:WaitForChild("Auras"))

local invFrame = script.Parent.Parent.Parent:WaitForChild("Tabs").Inventory:WaitForChild("ScrollingFrame")
local collectionFrame = script.Parent.Parent.Parent:WaitForChild("Tabs").Collection:WaitForChild("ScrollingFrame")
local exampleBtn = invFrame:WaitForChild("Example")

local db = false

local specialChance = 1000
local cutsceneChance = 10000

local function auraTick(nameTxt : TextLabel, chanceTxt : TextLabel)
	local selectedAura = math.random(1, #Auras)
	
	local aura = Auras[selectedAura]
	
	nameTxt.Text = tostring(aura.name)
	nameTxt.Position = UDim2.fromScale(.5, .46)
	nameTxt.TextColor3 = aura.color
	nameTxt.FontFace = aura.font
	game.TweenService:Create(nameTxt, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), { Position = UDim2.fromScale(.5, .48)}):Play()
	
	chanceTxt.Text = "1 in " .. tostring(aura.chance)
	chanceTxt.Position = UDim2.fromScale(.5, .515)
	chanceTxt.TextColor3 = aura.color
	chanceTxt.FontFace = aura.font
	game.TweenService:Create(chanceTxt, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), { Position = UDim2.fromScale(.5, .535)}):Play()
	
	ss:WaitForChild("Tick"):Play()
end

button.MouseButton1Click:Connect(function()
	if db == false and game.Players.LocalPlayer:GetAttribute("AutoRoll") == false then
		db = true
		
		rollEvent:FireServer()
	end	
end)

rollEvent.OnClientEvent:Connect(function(result, chance, color, font)
	local blackFrame = script.Parent.Parent.Frame
	local star = blackFrame.Star
	local vignette = blackFrame.Vignette
	
	local nameTxt = blackFrame.AuraName
	local chanceTxt = blackFrame.AuraChance
	
	local skipButton = blackFrame.SkipAura
	local equipButton = blackFrame.EquipAura
	
	local skipClicked = false
	local equipClicked = false
	
	local cooldown = game.Players.LocalPlayer:GetAttribute("Cooldown")
	
	blackFrame.Visible = true
	
	auraTick(blackFrame.AuraName, blackFrame.AuraChance)
	task.wait(cooldown - 0.25)
	auraTick(blackFrame.AuraName, blackFrame.AuraChance)
	task.wait(cooldown - 0.25)
	auraTick(blackFrame.AuraName, blackFrame.AuraChance)
	task.wait(cooldown - 0.25)
	auraTick(blackFrame.AuraName, blackFrame.AuraChance)
	task.wait(cooldown - 0.25)
	
	nameTxt.Text = tostring(result)
	nameTxt.Position = UDim2.fromScale(.5, .46)
	nameTxt.TextColor3 = color
	nameTxt.FontFace = font
	game.TweenService:Create(nameTxt, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), { Position = UDim2.fromScale(.5, .48)}):Play()

	chanceTxt.Text = "1 in " .. tostring(chance)
	chanceTxt.Position = UDim2.fromScale(.5, .515)
	chanceTxt.TextColor3 = color
	chanceTxt.FontFace = font
	game.TweenService:Create(chanceTxt, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), { Position = UDim2.fromScale(.5, .535)}):Play()

	ss:WaitForChild("Tick"):Play()
	task.wait(cooldown - 0.25)
	
	if chance >= specialChance and chance < cutsceneChance then
		blackFrame.Transparency = 0
		vignette.Visible = true
		vignette.ImageTransparency = 1
		vignette.ImageColor3 = color
		
		game:GetService("TweenService"):Create(vignette, TweenInfo.new(4, Enum.EasingStyle.Sine, Enum.EasingDirection.In), { ImageTransparency = 0 }):Play()
		
		task.wait(4)
		ss:WaitForChild("NormalBoom"):Play()
		
		blackFrame.Transparency = 0.45
		vignette.ImageColor3 = Color3.fromRGB(255, 255, 255)
		vignette.ImageTransparency = 1
	elseif chance >= cutsceneChance then
		ss:WaitForChild("Shine"):Play()
		
		blackFrame.Transparency = 0
		star.Visible = true
		vignette.Visible = true
		
		star.ImageColor3 = color
		vignette.ImageColor3 = color
		vignette.ImageTransparency = 0
			
		star:TweenSize(UDim2.fromScale(0.5, 0.5), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 4)
		game:GetService("TweenService"):Create(star, TweenInfo.new(8, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), { Rotation = math.random(360, 360*1.5) }):Play()
		task.wait(4)
		star:TweenSize(UDim2.fromScale(1, 1), Enum.EasingDirection.In, Enum.EasingStyle.Sine, 4)
		task.wait(4)
		
		ss:WaitForChild("SpecialBoom"):Play()
		blackFrame.Transparency = 0.45
		vignette.Visible = false
		vignette.ImageColor3 = Color3.fromRGB(255, 255, 255)
		vignette.ImageTransparency = 1
		star.Visible = false
		star.Size = UDim2.fromScale(1, 1)
		star.Rotation = 0
		star.ImageColor3 = Color3.fromRGB(255, 255, 255)
	end
	
	if tonumber(game.Players.LocalPlayer:GetAttribute("AutoSkip")) < chance then
		if (tonumber(game.Players.LocalPlayer:GetAttribute("AutoEquip")) > chance) or (game.Players.LocalPlayer.Auras.Value == game.Players.LocalPlayer.Max.Value) then
			skipButton.Visible = true
			equipButton.Visible = true
		elseif tonumber(game.Players.LocalPlayer:GetAttribute("AutoEquip")) < chance and game.Players.LocalPlayer.Auras.Value ~= game.Players.LocalPlayer.Max.Value then
			print("Equip: " .. tostring(result))
			equipEvent:FireServer(result)
			
			mainEvent:FireServer(1, "aurasValue")
			
			-- create a button
			local btn = exampleBtn:Clone()
			btn.Parent = invFrame
			btn.Visible = true
			btn.Text = tostring(result)
			btn.TextColor3 = color
			btn.UIStroke.Color = color
			btn.FontFace = font
			btn.Chance.Value = chance

			local auraBtn : TextButton = collectionFrame[tostring(result)]
			auraBtn.Locked.Value = false
			auraBtn.Text = tostring(result)
			auraBtn.TextColor3 = color
			auraBtn.FontFace = font
			auraBtn.Interactable = true

			print("Weighted Result: " .. result)
			
			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false
			
			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		end
	else
		print("Weighted Result: " .. result)
		
		mainEvent:FireServer(1, "roll")

		blackFrame.Visible = false
		skipButton.Visible = false
		equipButton.Visible = false

		script.Parent.Frame.Transparency = 0.45
		script.Parent.Frame.Size = UDim2.fromScale(1, 1)
		game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

		task.wait(cooldown)
		db = false
	end
	
	local equippedAnAura = false
	local equipWarned = false
	local showedEquipWarning = false
	
	skipButton.MouseButton1Click:Connect(function()
		local warned = false
		if chance > specialChance then
			blackFrame.Warning.Visible = true
			warned = true
		end
		if warned == true then
			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false

			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		else
			print("Skipped: " .. tostring(result))
			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false
			
			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		end
	end)
	
	equipButton.MouseButton1Click:Connect(function()
		if game.Players.LocalPlayer.Auras.Value ~= game.Players.LocalPlayer.Max.Value and equippedAnAura == false then
			equippedAnAura = true
			
			print("Equip: " .. tostring(result))
			equipEvent:FireServer(result)
			
			mainEvent:FireServer(1, "aurasValue")
			
			-- create a button
			local btn = exampleBtn:Clone()
			btn.Parent = invFrame
			btn.Visible = true
			btn.Text = tostring(result)
			btn.TextColor3 = color
			btn.UIStroke.Color = color
			btn.FontFace = font
			btn.Chance.Value = chance
			
			local auraBtn : TextButton = collectionFrame[tostring(result)]
			auraBtn.Locked.Value = false
			auraBtn.Text = tostring(result)
			auraBtn.TextColor3 = color
			auraBtn.FontFace = font
			auraBtn.Interactable = true
			
			print("Weighted Result: " .. result)

			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			blackFrame.Warning.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false
			
			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		elseif game.Players.LocalPlayer.Auras.Value == game.Players.LocalPlayer.Max.Value and equipWarned == false then
			equipWarned = true
		elseif equipWarned == true then
			blackFrame.Warning.Visible = true
			blackFrame.Warning.Text = "Not enough storage! (if you click equip, a random aura from your inventory will get deleted)"
			showedEquipWarning = true
		elseif game.Players.LocalPlayer.Auras.Value == game.Players.LocalPlayer.Max.Value and showedEquipWarning == true then
			for _, child in pairs(invFrame:GetChildren()) do
				if child:IsA("TextButton") then
					child:Destroy()
					mainEvent:FireServer(-1, "aurasValue")
					break
				end
			end
			
			print("Equip: " .. tostring(result))
			equipEvent:FireServer(result)
			
			-- create a button
			local btn = exampleBtn:Clone()
			btn.Parent = invFrame
			btn.Visible = true
			btn.Text = tostring(result)
			btn.TextColor3 = color
			btn.UIStroke.Color = color
			btn.FontFace = font
			btn.Chance.Value = chance
			
			local auraBtn : TextButton = collectionFrame[tostring(result)]
			auraBtn.Locked.Value = false
			auraBtn.Text = tostring(result)
			auraBtn.TextColor3 = color
			auraBtn.FontFace = font
			auraBtn.Interactable = true
			
			print("Weighted Result: " .. result)

			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false
			
			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		end
	end)
end)
skipButton.MouseButton1Click:Connect(function()
		local warned = false
		if chance > specialChance then
			blackFrame.Warning.Visible = true
			warned = true
		end
        equippedAnAura = false -- Added this to reset equipping an aura.
		if warned == true then
			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false

			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		else
			print("Skipped: " .. tostring(result))
			mainEvent:FireServer(1, "roll")
			
			blackFrame.Visible = false
			skipButton.Visible = false
			equipButton.Visible = false
			
			script.Parent.Frame.Transparency = 0.45
			script.Parent.Frame.Size = UDim2.fromScale(1, 1)
			game.TweenService:Create(script.Parent.Frame, TweenInfo.new(cooldown), { Transparency = 1, Size = UDim2.fromScale(0, 1) }):Play()

			task.wait(cooldown)
			db = false
		end
	end)```

Could you try this