If Then Statement will only work once

What do you want to achieve? Keep it simple and clear!
I want script to smoothly fire multiple times.
What is the issue? Include screenshots / videos if possible!
image
The print script is firing multiple times which might cause it to fail. I think it may have to do with the script looping.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve rearranged the if statements and removed the other bugs in my old script. Everything works fine but only one time.

function SlotReload1() --- FadeOut for Image1 Effect
	time = 10 -- time for reload
	if time == 10 then
		slot1.ReloadCounter.TextTransparency = 0
		slot1.ReloadCounter.UIStroke.Transparency = 0
		repeat
			slot1.ReloadCounter.Text = time
			time = time - 1 
			wait(1)
		until time == 0
		slot1.ReloadCounter.TextTransparency = 1
		slot1.ReloadCounter.UIStroke.Transparency = 1
	end
end

function FadeOut()
	for i = 1,25 do
		wait()
		slot1.ReloadFrame.ImageTransparency = (0+(0.05*i))
	end
end

-- Knife Action--
uis.InputBegan:connect(function(input)
	if uis:IsKeyDown(Enum.KeyCode.Q) and localplayer.hiddenstats.Murderer.Value == true and time == 0 then
		local TouchPart = localplayer.Character.HumanoidRootPart.KillSphere
		print('lets see if this is working')
		TouchPart.Touched:Connect(function(touched)
			if uis:IsKeyDown(Enum.KeyCode.Q) and localplayer.hiddenstats.Murderer.Value == true and touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") and time == 0 then
				print('dude amogus in real life, the fitness gram pacer test is a multi tasked arobic test')
				stabsound:Play()
				spawn(FadeOut)
						local Player = players:GetPlayerFromCharacter(touched.Parent)
						if Player then
							-- do something here
						end
					end
		end)
		spawn(SlotReload1)
	end
end)
1 Like

Touched items can fire many times very quickly. Normally a debounce is used to control it.

2 Likes

i attempted to use a debounce in my if statement. It makes the print go off only once. But it still will only work one time. Heres my script now:

local players = game.Players
local localplayer = players.LocalPlayer
local inventorygui = script.Parent.InventoryGui
local slot1 = inventorygui.InventorySlot1
local uis = game:GetService("UserInputService")
local slotactive = false
local time = 0
local stabsound = script.StabSound
local killcyl = workspace.KillSizePreview
local debounce = true
slot1.ReloadCounter.TextTransparency = 1
slot1.ReloadCounter.UIStroke.Transparency = 1
slot1.ReloadFrame.ImageTransparency = 1

function SlotReload1() --- FadeOut for Image1 Effect
	time = 10 -- time for reload
	if time == 10 then
		slot1.ReloadCounter.TextTransparency = 0
		slot1.ReloadCounter.UIStroke.Transparency = 0
		repeat
			slot1.ReloadCounter.Text = time
			time = time - 1 
			wait(1)
		until time == 0
		debounce = true
		slot1.ReloadCounter.TextTransparency = 1
		slot1.ReloadCounter.UIStroke.Transparency = 1
	end
end

function FadeOut()
	for i = 1,25 do
		wait()
		slot1.ReloadFrame.ImageTransparency = (0+(0.05*i))
	end
end

-- Knife Action--
uis.InputBegan:connect(function(input)
	if uis:IsKeyDown(Enum.KeyCode.Q) and localplayer.hiddenstats.Murderer.Value == true and time == 0 then
		local TouchPart = localplayer.Character.HumanoidRootPart.KillSphere
		print('lets see if this is working')
		TouchPart.Touched:Connect(function(touched)
			if uis:IsKeyDown(Enum.KeyCode.Q) and localplayer.hiddenstats.Murderer.Value == true and touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") and debounce == true then
				print('dude amogus in real life, the fitness gram pacer test is a multi tasked arobic test')
				debounce = false
				stabsound:Play()
				spawn(FadeOut)
						local Player = players:GetPlayerFromCharacter(touched.Parent)
						if Player then
							-- do something here
						end
					end
		end)
		spawn(SlotReload1)
	end
end)
1 Like

Pretty sure it’s because you need a task.wait(time to delay) inside your check to see if q is pressed and you need to reset debounce = false there too. The debounce link I posted before should explain why.

1 Like

It’s because you need to disconnect the function after. Nothing to do with debounces.

Fixed script:

local connection = nil

local function SlotReload1() --- FadeOut for Image1 Effect
	time = 10 -- time for reload
	if time == 10 then
		slot1.ReloadCounter.TextTransparency = 0
		slot1.ReloadCounter.UIStroke.Transparency = 0
		
		repeat
			slot1.ReloadCounter.Text = time
			time -= 1
			task.wait(1)
		until time == 0
		
		slot1.ReloadCounter.TextTransparency = 1
		slot1.ReloadCounter.UIStroke.Transparency = 1
	end
end

local function FadeOut()
	for i = 1, 25 do
		task.wait()
		slot1.ReloadFrame.ImageTransparency = (0+(0.05*i))
	end
end

-- Knife Action--
uis.InputBegan:connect(function(input)
	if uis:IsKeyDown(Enum.KeyCode.Q) and localplayer.hiddenstats.Murderer.Value and time == 0 then
		if connection then
			connection:Disconnect()
			connection = nil
		end
		
		local TouchPart = localplayer.Character.HumanoidRootPart.KillSphere
		print('lets see if this is working')
		connection = TouchPart.Touched:Connect(function(touched)
			if uis:IsKeyDown(Enum.KeyCode.Q) and localplayer.hiddenstats.Murderer.Value == true and touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") and time == 0 then
				print('dude amogus in real life, the fitness gram pacer test is a multi tasked arobic test')
				stabsound:Play()
				task.spawn(FadeOut)
				
				local Player = players:GetPlayerFromCharacter(touched.Parent)
				
				if Player then
					-- do something here
				end
			end
		end)
		
		task.spawn(SlotReload1)
	end
end)
2 Likes

Thank you a bunch! But now the second print command will not show, and nothing else will fire will work in its segmant. Theres no errors, and i dont know how that could’ve happened

Could you expand on this? I don’t fully know where to place the debounce = false.