Hotbar Invisible when spam clicking on it

  1. What do you want to achieve?
    Custom Hotbar

  2. What is the issue?
    The issue is when i spam pressed “1” or spam click the hotbar(keys for equipping tools and unequipping tools), the hotbar will goes invisible

  1. What solutions have you tried so far?
    Searched on other social medias, but no results

I had 2 scripts and 1 for server and 1 for client

Client

game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

local Equipped = false
local Debounce = true

local plr = game.Players.LocalPlayer
local char = plr.Character

local backpack = plr:WaitForChild("Backpack")


local tools = backpack:GetChildren()


local slotMax = 4


local hotbar = script.Parent


local numToWord = 
	{
		[1] = "One",
		[2] = "Two",
		[3] = "Three",
		[4] = "Four"
	}


local function updateHotbar()


	for i, child in pairs(hotbar:GetChildren()) do

		if child.Name == "Slot" then child:Destroy() end
	end


	for i, tool in pairs(tools) do


		if i > slotMax then return end


		local slotClone = script.Slot:Clone()
		slotClone.SlotsNumber.Text = i


		slotClone.Image = tool.TextureId


		slotClone.Parent = hotbar
		
		slotClone.MouseButton1Click:Connect(function()
			game.ReplicatedStorage.EquipToolRE:FireServer(tool, tool.Parent)
		end)

		game:GetService("UserInputService").InputBegan:Connect(function(input, processed)

			if not processed then

				if input.KeyCode == Enum.KeyCode[numToWord[i]] then
					if Debounce then
						Debounce = false
						if Equipped then
							Equipped = false
						elseif not Equipped then
							Equipped = true
						end
						
						game.ReplicatedStorage.EquipToolRE:FireServer(tool, tool.Parent)			
						task.wait(.8)
						Debounce = true
					end
				end
			end
		end)
	end
end

updateHotbar()


backpack.ChildAdded:Connect(function(child)

	if not table.find(tools, child) then
		table.insert(tools, child)
		updateHotbar()
	end
end)

backpack.ChildRemoved:Connect(function(child)

	if child.Parent ~= char then
		table.remove(tools, tools[child])
		updateHotbar()
	end
end)


char.ChildAdded:Connect(function(child)

	if child:IsA("Tool") and not table.find(tools, child) then
		table.insert(tools, child)
		updateHotbar()
	end
end)

char.ChildRemoved:Connect(function(child)

	if child.Parent ~= backpack then
		table.remove(tools, tools[child])
		updateHotbar()
	end
end)



game.ReplicatedStorage.EquipToolRE.OnClientEvent:Connect(function()


	for x, slot in pairs(hotbar:GetChildren()) do


		if slot:IsA("Frame") then

			local tool = tools[tonumber(slot.HotkeyNumber.Text)]

			if tool.Parent ~= char then

				slot.BackgroundColor3 = Color3.fromRGB(57, 57, 57)

			else
				slot.BackgroundColor3 = Color3.fromRGB(88, 88, 88)
			end
		end
	end
end)

Server :

game.ReplicatedStorage.EquipToolRE.OnServerEvent:Connect(function(player, tool, parent)
	local char = player.Character
	if char then
		if parent ~= char then
			char.Humanoid:UnequipTools()
			char.Humanoid:EquipTool(tool)
		else
			tool.Parent = player.Backpack
		end

		game.ReplicatedStorage.EquipToolRE:FireClient(player)
	end
end)

i don’t know why you have to fire the client with the fired server in order to change the background since that creates a lot of memory which will add some frame delay, when clients fire a server it’s much faster and server can receive it faster but when server fires it it has some slight frame delay so the client doesn’t instantly get it

im thinking that your auto clicker clicks faster than the client event registers so try to create some optimizations,

the input began function has a delay for 0.8 seconds but this one doesn’t im assuming its also a problem

1 Like

So, I think you should take rest and try again next time :sweat_smile:
you gonna need a lot of testing i guess.

maybe :
1.

put them outside of updateHotbar

I believe this will not work on real live game because you’re comparing instances here. parent(it cannot be instance)
Edit : i’m wrong here(since when roblox made update, I never know about this :sweat_smile:)

overall, your code look fine. i’m not sure what is the problem is. :sweat_smile:

1 Like

Chances are it’s because they are swapping between the Backpack and Character so quickly, it overloads?
I’d put a,

repeat wait() until player.Backpack.SwordName

But I could be completely wrong.

local rs = game:GetService("ReplicatedStorage")
local equipRE = rs:WaitForChild("EquipToolRE")
local hotbar = nil --define this in the server script

equipRE.OnServerEvent:Connect(function(player, tool, toolParent)
	local char = player.Character
	local backpack = player.Backpack
	local tools = backpack:GetChildren()
	for _, slot in ipairs(hotbar:GetChildren()) do
		if slot:IsA("Frame") then
			local tool = tools[tonumber(slot.HotkeyNumber.Text)]
			if tool.Parent ~= char then
				slot.BackgroundColor3 = Color3.fromRGB(57, 57, 57)
			else
				slot.BackgroundColor3 = Color3.fromRGB(88, 88, 88)
			end
		end
	end
end)

Your issue was attempting to use an “OnClientEvent” event listener inside a server script, when you fire the server by calling “FireServer()” through a RemoteEvent instance from any local script, any corresponding OnServerEvent event listener through the same RemoteEvent instance in any server script is fired, you also forgot the parameters which its connected callback function should receive to handle the arguments passed to the FireServer() call and subsequently to the callback.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local backpack = plr:WaitForChild("Backpack")
local tools = backpack:GetChildren()

local rs = game:GetService("ReplicatedStorage")
local equipRE = rs:WaitForChild("EquipToolRE")

local uis = game:GetService("UserInputService")

local debounce = false
local equipped = false

local slotMax = 4
local hotbar = script.Parent

local numToWord = 
	{
		[1] = "One",
		[2] = "Two",
		[3] = "Three",
		[4] = "Four"
	}


local function updateHotbar()
	for i, child in ipairs(hotbar:GetChildren()) do
		if child.Name == "Slot" then child:Destroy() end
	end

	for i, tool in ipairs(tools) do
		if i > slotMax then return end
		local slotClone = script.Slot:Clone()
		slotClone.SlotsNumber.Text = i
		slotClone.Image = tool.TextureId
		slotClone.Parent = hotbar
		slotClone.MouseButton1Click:Connect(function()
			equipRE:FireServer(tool, tool.Parent)
		end)
		
		uis.InputBegan:Connect(function(input, processed)
			if processed then
				return
			end
			if debounce then
				return
			end
			if input.KeyCode == Enum.KeyCode[numToWord[i]] then
				debounce = true
				equipped = not equipped
				equipRE:FireServer(tool, tool.Parent)			
				task.wait(.8)
				debounce = false
			end
		end)
	end
end

updateHotbar()

backpack.ChildAdded:Connect(function(child)
	if not table.find(tools, child) then
		table.insert(tools, child)
		updateHotbar()
	end
end)

backpack.ChildRemoved:Connect(function(child)
	if child.Parent ~= char then
		if table.find(tools, child) then
			table.remove(tools, table.find(tools, child))
		end
		updateHotbar()
	end
end)


char.ChildAdded:Connect(function(child)
	if child:IsA("Tool") and not table.find(tools, child) then
		table.insert(tools, child)
		updateHotbar()
	end
end)

char.ChildRemoved:Connect(function(child)
	if child.Parent ~= backpack then
		if table.find(tools, child) then
			table.remove(tools, table.find(tools, child))
		end
		updateHotbar()
	end
end)

The table.remove() calls were done incorrectly. Some other slight changes added too.

Just tested it out but still doesn’t work
Also thanks for help

Thanks for help out. So I Tested it out but still doesn’t work

So I tested it out and it seems like to be working! Thank you very much for helping I appreciated it. :slight_smile:

I know it’s already solved. But just want to know it is true it’s already fixed because your code seem fine.

another problem I found :

I believe it an imageButton not Frame

slot:IsA("Frame")  --change to slot:IsA("ImageButton")

also here

slot.HotkeysNumber  --change to slot.SlotsNumber

also here both of them work, roblox made an update i guess,

table.remove(tools, tools[child])
--or
table.remove(tools,table.find(tools,child))

overall, i not sure what’s the problem of it because all seem fine.
if it’s already solve then that’s good. :slight_smile:

hmmm i don’t remember making those codes :thinking::thinking:

But thanks for helping me

Your joking right. :no_mouth:

bye.