I’ve literally spent hours messing around with debounce all over in my weld script just to have nothing happen.
Now I’ve noticed if anything - I need to add a cooldown to how soon you can equip a tool, is there any way I can implement this?
The guns in my game have been getting spammed to cause lag and I’m just trying to prevent this - any help is appreciated
CURRENT WELD SCRIPT FOR TOOL
local player = game.Players.LocalPlayer
local rCFrame = CFrame.new(-1,0.3,-1.08)*CFrame.Angles(math.rad(-90),math.rad(-5),0)
local lCFrame = CFrame.new(1,0.3,-1.08)*CFrame.Angles(math.rad(-90),math.rad(55),0)
local armModel = nil
function Weld(x,y)
local W = Instance.new("Weld")
W.Part0 = x
W.Part1 = y
local CJ = CFrame.new(x.Position)
local C0 = x.CFrame:inverse()*CJ
local C1 = y.CFrame:inverse()*CJ
W.C0 = C0
W.C1 = C1
W.Parent = x
end
function Get(A)
if A.className == "Part" then
Weld(script.Parent.Handle, A)
A.Anchored = false
else
local C = A:GetChildren()
for i=1, #C do
Get(C[i])
end
end
end
function Finale()
Get(script.Parent)
--game.ReplicatedStorage.Events.ToolWelds:FireServer(true, script.Parent)
end
function addFakeArms()
local fakeArms = {nil,nil}
if armModel == nil then
armModel = Instance.new("Model", workspace)
armModel.Name = "ArmModel"
end
if script.Parent.Parent:FindFirstChild("Right Arm") then
fakeArms[1] = script.Parent.Parent["Right Arm"]:clone()
fakeArms[1].FormFactor = "Custom"
fakeArms[1].Name = "Right Arm"
fakeArms[1].Transparency = 0.5
fakeArms[1].Size = Vector3.new(0.2,0.2,0.2)
local rWeld = Instance.new("Weld",fakeArms[1])
rWeld.Part0 = fakeArms[1]
rWeld.Part1 = script.Parent.Parent["Right Arm"]
fakeArms[1].Parent = armModel
local a = Instance.new("BlockMesh",fakeArms[1])
a.Scale = Vector3.new(4.8,9.8,4.8)
end
if script.Parent.Parent:FindFirstChild("Left Arm") then
fakeArms[2] = script.Parent.Parent["Left Arm"]:clone()
fakeArms[2].FormFactor = "Custom"
fakeArms[2].Name = "Left Arm"
fakeArms[2].Transparency = 0.5
fakeArms[2].Size = Vector3.new(0.2,0.2,0.2)
local rWeld = Instance.new("Weld",fakeArms[2])
rWeld.Part0 = fakeArms[2]
rWeld.Part1 = script.Parent.Parent["Left Arm"]
fakeArms[2].Parent = armModel
local a = Instance.new("BlockMesh",fakeArms[2])
a.Scale = Vector3.new(4.8,9.8,4.8)
end
end
script.Parent.Equipped:connect(function()
script.Parent.Enabled = false
wait(5)
script.Parent.Enabled = true
Finale()
game.ReplicatedStorage.Events.ToolWelds:FireServer(script.Parent, true, rCFrame, lCFrame)
addFakeArms()
end)
script.Parent.Unequipped:connect(function()
Finale()
if armModel then
armModel:Destroy()
armModel = nil
end
game.ReplicatedStorage.Events.ToolWelds:FireServer(script.Parent, false)
end)
script.Parent:WaitForChild("Handle")
Finale()
local debounce = false
--a function's beginning
if debounce == true then return end
debounce = true
--more coding of yours
wait()--can be any long
debounce = false
You have to do this yes, also, remove the wait when you equip the gun, or else you can spam the living daylights out of it, and it’ll have 500 things on a timer
Model = game.ServerStorage['AK'] --Model you want to destroy
for _, Child in pairs(Model:GetChildren()) do
Child:Clone().Parent = script.Parent--Move the children up one parent
end
however when I added a wait(2) it never ended up transferring it over
Oh no, no need to use that for loop, you simply clone the model and parent it to the player’s character, while also touching the welding stuff.
The only weld you need to leave empty is the one needed to weld the player’s arms to the gun, which will be done by the script’s code.
By the way, I suggest using a bool value for the cooldown, so it’s server sided and secured from cheaters as well.
player.CharacterAdded:Connect(function(character)
local cooldownvalue = Instance.new("BoolValue", character)
cooldownvalue.Name = "" --any name you want
end)
In coding, there are many methods users can use to achieve the same outcome. I don’t agree with most of the suggestions the users have provided, so I’m going to suggest my own input that I always use with no hassle.
If you want a proper cooldown to prevent spam, a simple local debounce will do just fine.
You want to make sure outside of any function,
local debounce = false
–next will go your function, in this case its when your equips a weapon or whatever item.
if not debounce then
– an if statement to check if debounce is false then it will run.
–inside the if statement, you need to make sure your debounce is set to true. Then you implement the code that makes your function, ‘do stuff’.
–include a wait(“seconds”) then at the end of the if statement add, debounce = false
this is because the function needs to check if debounce is false in order to run it again. If the debounce is false then the function will run again when the event is triggered.
For reference, you can also use ROBLOX’s practice script for debouncing.
```
--Store whether the button is pressed in a local variable
local buttonPressed = false
script.Parent.Touched:connect(function(hit)
-- Is it not pressed?
if not buttonPressed then
-- Mark it as pressed, so that other handlers don't execute
buttonPressed = true
if hit.Parent then
hum = hit.Parent:FindFirstChild("Humanoid")
hum.Health = hum.Health - 5
end
wait(3)
-- Mark it as not pressed, so other handlers can execute again
buttonPressed = false
end
end)
indent preformatted text by 4 spaces
```
Not going to lie - I’m not the most experienced scripter. I’m trying to find a simple route that I can implement it into all of the guns
Script to make wait time work: [SCRIPT 1]
if script.Parent.AK.Disabled == true then
print("AK Disabled is true")
wait (5)
print("Waited 5 seoonds")
script.Parent.AK.Disabled = false
print("AK is now enabled.")
end
I implemented a script to do this seeing that whenever I added the wait(x) function on this script:
SCRIPT 2:
Model = game.ServerStorage['AK'] --Model you want to destroy
for _, Child in pairs(Model:GetChildren()) do
Child:Clone().Parent = script.Parent--Move the children up one parent
end
It wouldn’t allow the script to run at all.
When attempting
script.Parent.Equipped:connect(function()
The script wouldn’t work at all either. Essentially Script 1 works - but what I noticed is that it will run the wait time and enable Script 2 without the tool actually being equipped. Instead it will begin to enable it once it is in the player’s backpack.
Is there anyway to edit any of the current scripts I’ve just provided in order to make a wait time work? I’m looking to make the player wait at least [2] seconds before they actually have the weapon fully equipped.
Now, i wouldn’t usually provide code, but you should weld the gun instead of welding it upon it being equipped it will cause obvious problems down the road, weld it with a script in studio, and you should be fine with your weld issue, on the other hand, your debounce is really badly used as it is just a wait(5) make a variable outside of the scope, also, dont disable the tool, it will cause issues i think that it will cause alot of things to happen out of place, when you enable or disable something, your killing the descendants of the tool or anything that you are disabling/enabling, try something like this
local equipping = false -- this should be used outside of the .Equipped signal connection function scope, also define what the type is, so you dont get errors randomly, sometimes
equipping = not equipping -- this is the same as equipping = true
wait(1) -- this can be however long you want it to be
equipping = not equipping -- this is the same as equipping = false
-- perform your equip code below, should work as expected, also destroy welds after uneqipping or equipping to make sure you dont weld an already welded part BAD things will happen!