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
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
So, I think you should take rest and try again next time
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 )
overall, your code look fine. i’m not sure what is the problem is.
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.