What do you want to achieve? Keep it simple and clear!
I want to make sure the tools do not disappear when I switch weapons too fast.
What is the issue? Include screenshots / videos if possible!
So I am making an inventory system like in Counter Strike, and currently the only issue I have is that when I spam the buttons to change the tools, it makes the guns glitch out and not weld to the player’s arm, making it fall into the void and destroying it, so I can’t equip it again.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried to first unequip the tool before changing it and equipping a new one. Didn’t help
Video Example:
Code that Equips the tool:
local function ChangeSelected(Type)
if Selected == Type or Humanoid:GetState() == Enum.HumanoidStateType.Dead then return end
if Type ~= "UtilityHammer" and Type ~= "UtilityBandage" then
local Frame = GUI.Main:FindFirstChild(Type)
local Gradient: UIGradient = Frame:FindFirstChildWhichIsA("UIGradient")
local Found = false
for _,v in Player.Backpack:GetChildren() do
if not v:IsA("Tool") then continue end
if v:GetAttribute("Type") == Type then
if Character:FindFirstChildWhichIsA("Tool") then
Humanoid:UnequipTools()
end
Humanoid:EquipTool(v)
Found = true
break
end
end
if not Frame or not Gradient or not Found then return end
Gradient.Color = ColorSequence.new(Color3.fromRGB(139,139,139))
for _,v in GUI.Main:GetChildren() do
if not v:IsA("Frame") or v.Name == "Utility" or v == Frame then continue end
local OtherGradient = v:FindFirstChildWhichIsA("UIGradient")
if not OtherGradient then continue end
OtherGradient.Color = ColorSequence.new(Color3.fromRGB(0,0,0))
end
elseif Type == "UtilityHammer" or Type == "UtilityBandage" then
local Found = false
for _,v in Player.Backpack:GetChildren() do
if not v:IsA("Tool") then continue end
if v:GetAttribute("Type") == Type then
if Character:FindFirstChildWhichIsA("Tool") then
Humanoid:UnequipTools()
end
Humanoid:EquipTool(v)
Found = true
break
end
end
if not Found then return end
for _,v in GUI.Main:GetChildren() do
if not v:IsA("Frame") or v.Name == "Utility" then continue end
local Gradient = v:FindFirstChildWhichIsA("UIGradient")
if not Gradient then continue end
Gradient.Color = ColorSequence.new(Color3.fromRGB(0,0,0))
end
end
Selected = Type
if Sound.IsPlaying then Sound:Stop() end
Sound:Play()
end
if the issue is that you go flying up, turn the collision off with the tools, oh yes i see that, try adding a cooldown (will show a script for it soon)
local debounce = false
local function ChangeSelected(Type)
if Selected == Type or Humanoid:GetState() == Enum.HumanoidStateType.Dead then return end
if Type ~= "UtilityHammer" and Type ~= "UtilityBandage" then
if debounce = false then
debounce = true
local Frame = GUI.Main:FindFirstChild(Type)
local Gradient: UIGradient = Frame:FindFirstChildWhichIsA("UIGradient")
local Found = false
for _,v in Player.Backpack:GetChildren() do
if not v:IsA("Tool") then continue end
if v:GetAttribute("Type") == Type then
if Character:FindFirstChildWhichIsA("Tool") then
Humanoid:UnequipTools()
end
Humanoid:EquipTool(v)
Found = true
break
end
end
if not Frame or not Gradient or not Found then return end
Gradient.Color = ColorSequence.new(Color3.fromRGB(139,139,139))
for _,v in GUI.Main:GetChildren() do
if not v:IsA("Frame") or v.Name == "Utility" or v == Frame then continue end
local OtherGradient = v:FindFirstChildWhichIsA("UIGradient")
if not OtherGradient then continue end
OtherGradient.Color = ColorSequence.new(Color3.fromRGB(0,0,0))
end
elseif Type == "UtilityHammer" or Type == "UtilityBandage" then
local Found = false
for _,v in Player.Backpack:GetChildren() do
if not v:IsA("Tool") then continue end
if v:GetAttribute("Type") == Type then
if Character:FindFirstChildWhichIsA("Tool") then
Humanoid:UnequipTools()
end
Humanoid:EquipTool(v)
Found = true
break
end
end
if not Found then return end
for _,v in GUI.Main:GetChildren() do
if not v:IsA("Frame") or v.Name == "Utility" then continue end
local Gradient = v:FindFirstChildWhichIsA("UIGradient")
if not Gradient then continue end
Gradient.Color = ColorSequence.new(Color3.fromRGB(0,0,0))
end
end
Selected = Type
if Sound.IsPlaying then Sound:Stop() end
Sound:Play()
task.wait(0.3)
debounce = false
end
end
[/quote]
(fix the positioning of the debounce if you want its a studio bug)
You can add the cooldown, It should fix a problem, like a 0.5 delay
\
local cooldown = false
local function ChangeSelected(Type)
if Selected == Type or Humanoid:GetState() == Enum.HumanoidStateType.Dead or cooldown then return end
cooldown = true
game:GetService("Debris"):AddItem(workspace.DummyObject, 0.5)
game:GetService("Debris").ItemDestroyed:Connect(function()
cooldown = false
end)
--
Selected = Type
if Sound.IsPlaying then Sound:Stop() end
Sound:Play()
end
It still disappears when I do it. I’ve checked, and theres no scripts that destroy the tool. IDK why it makes it collidable in the first place. maybe that is related to the issue.
EDIT: Wait, I found out that it just doesn’t let me change the items; they are still in my back pack. It is related to scrolling.
Here is the code that runs this function:
local function HandleActions(ActionName, InputState, InputObject)
if (ActionName == "Melee" or ActionName == "Secondary" or ActionName == "Primary" or ActionName == "UtilityHammer" or ActionName == "UtilityBandage") and GUI.Enabled and InputState == Enum.UserInputState.Begin then
ChangeSelected(ActionName)
elseif ActionName == "Scroll" and GUI.Enabled then
if InputObject.Position.Z > 0 then
print("scrollup")
if Selected == "Melee" then
ChangeSelected("Secondary")
elseif Selected == "Secondary" then
ChangeSelected("Primary")
end
else
print("scrolldown")
if Selected == "Primary" then
ChangeSelected("Secondary")
elseif Selected == "Secondary" then
ChangeSelected("Melee")
end
end
end
end
local function BindActions()
CAS:BindAction("Melee", HandleActions, false, Enum.KeyCode.Three)
CAS:BindAction("Secondary", HandleActions, false, Enum.KeyCode.Two)
CAS:BindAction("Primary", HandleActions, false, Enum.KeyCode.One)
CAS:BindAction("Scroll", HandleActions, false, Enum.UserInputType.MouseWheel)
CAS:BindAction("UtilityHammer",HandleActions, false, Enum.KeyCode.J)
CAS:BindAction("UtilityBandage", HandleActions, false, Enum.KeyCode.H)
end
BindActions()