I am making a button that whenever you click will fire a module and a remote event, the problem is that on one click the script will be fire hundreds of time and I don’t know how to solve this
while true do
task.wait()
for _, Bind in pairs(BindFrame:GetDescendants()) do
if Bind.Name == "NewGui" then
Bind.MouseButton1Down:Connect(function()
if Bind.Text == "Z" and Debounce ~= true then
Debounce = true
task.spawn(function()
EFPower:ZClient(character)
--Zevent:FireServer(Player)
Debounce = false
end)
end
here’s the module
function EFPower:ZClient(character)
local humanoid = character:FindFirstChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://11107911278"
Animation.Name = "Anim"
local WS = humanoid.WalkSpeed
Animation.Parent = humanoid
local AnimationTrack = humanoid:LoadAnimation(Animation)
AnimationTrack:Play()
humanoid.WalkSpeed = 0
ps: on the first script I use a while true do because there’s another four buttons with the same function but another key.
Do not make connection in a infinite loop, every :Connect run will create another function to run. You only need one connection per button, keep the for loop.
-- remove while true do
-- and task.wait()
for _, Bind in pairs(BindFrame:GetDescendants()) do
if Bind.Name == "NewGui" then
Bind.MouseButton1Down:Connect(function()
local Player = game:GetService("Players").LocalPlayer
local BindFrame = Player.PlayerGui:WaitForChild("ScreenGui").KeyBindFrame
local bindEvent = game:GetService("ReplicatedStorage"):WaitForChild("Powers")
local EFPower = require(game:GetService("ReplicatedStorage").Powers["Explosive Flash"].EFPower)
local tycoon = "Explosive Flash"
local character = Player.Character
local Zevent = bindEvent:FindFirstChild(tycoon).Zevent
local Cevent = bindEvent:FindFirstChild(tycoon).Cevent
local Xevent = bindEvent:FindFirstChild(tycoon).Xevent
local Vevent = bindEvent:FindFirstChild(tycoon).Vevent
local Fevent = bindEvent:FindFirstChild(tycoon).Fevent
local Debounce
while true do
task.wait()
for _, Bind in pairs(BindFrame:GetDescendants()) do
if Bind.Name == "NewGui" then
Bind.MouseButton1Down:Connect(function()
if Bind.Text == "Z" and Debounce ~= true then
Debounce = true
task.spawn(function()
EFPower:ZClient(character)
--Zevent:FireServer(Player)
Debounce = false
end)
end
if Bind.Text == "X" then
Xevent:FireServer(Player)
end
if Bind.Text == "C" then
Cevent:FireServer(Player)
end
if Bind.Text == "V" then
Vevent:FireServer(Player)
end
if Bind.Text == "F" then
Fevent:FireServer(Player)
end end)
end
end
end
You should absolutely remove the while loop, the other Bind.Text if statements do not need to be in a while loop because all off them are in the connected function.
The problem is that the script is connected to a tool, so whenevr the tool is created the buttons will also be created, so i don’t know if i remove the while loop this will work.
If the GUI is destroyed then the connections are also destroyed and you won’t have to worry. If you get an error post it here, the while loop is not the solution.
The gui buttons are created when the tool is equipped and destroyed whonever the tool is unequipped so if the script only runs only time he will never work
Could we move this script and it’s contents into the tool’s equipped function?
If not then we will have to add another event connection, this one more appropriate than every frame. Let’s try on child added for the bind frame, this way when the objects are added we immediately connect the appropriate events, no matter where they come from, and should behave like you have programmed before.
BindFrame.ChildAdded:Connect(function(child)
if child.Name == "NewGui" then
Bind.MouseButton1Down:Connect(function()
if Bind.Text == "Z" then
EFPower:ZClient(character)
--Zevent:FireServer(Player)
elseif Bind.Text == "X" then
Xevent:FireServer(Player)
elseif Bind.Text == "C" then
Cevent:FireServer(Player)
elseif Bind.Text == "V" then
Vevent:FireServer(Player)
elseif Bind.Text == "F" then
Fevent:FireServer(Player)
end
end)
end
end)
I put the function inside a tool equipped event with a wait() and it works, thank you, but you know how to do when the animations get played the player will continue on the position until I change and how to anchor the character?