If Statement in lua will only work one time

In hindsight you don’t actually need to take SlotReload1 out of the task.spawn(), though you also don’t need to keep it in there so long as you call SlotReload1 after spawning the FadeOut thread, since it’s a different thread.
The reason it is running multiple times is that it is no longer checking the timer in the touched event if statement. My solution would be to disconnect the touched event inside of the touched event

connection = TouchPart.Touched:Connect(function(touched)
	connection:Disconnect()
end)

This is, as usual, very hacky, but it solves a surprisingly large amount of the issues with your code.

1 Like

Im sorry if I may be frustrating but the loop is still repeating and slot1reload() doesnt activate. Do I need to put the disconnect inside of the if statement?

uis.InputBegan:connect(function(input)
	if uis:IsKeyDown(Enum.KeyCode.Q) and localplayer.hiddenstats.Murderer.Value and timer == 0 then
		
		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") then
				print('dude amogus in real life, the fitness gram pacer test is a multi tasked arobic test')
				stabsound:Play()
				timer = 10

				local Player = players:GetPlayerFromCharacter(touched.Parent)
				if Player then
					-- do something here
				end
				task.spawn(FadeOut)
			end
		end)
	end
end)

if connection then
	SlotReload1()
	connection:Disconnect()
end

Haha, no worries, you just need to put the disconnect at the very start of the touched event, and the call to SlotReload1 at the very end of the if statement block.

uis.InputBegan:connect(function(input)
	if uis:IsKeyDown(Enum.KeyCode.Q) and localplayer.hiddenstats.Murderer.Value and timer == 0 then
		
		local TouchPart = localplayer.Character.HumanoidRootPart.KillSphere
		print('lets see if this is working')
		connection = TouchPart.Touched:Connect(function(touched)
			connection:Disconnect()
			if uis:IsKeyDown(Enum.KeyCode.Q) and localplayer.hiddenstats.Murderer.Value == true and touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") then
				print('dude amogus in real life, the fitness gram pacer test is a multi tasked arobic test')
				stabsound:Play()
				timer = 10

				local Player = players:GetPlayerFromCharacter(touched.Parent)
				if Player then
					-- do something here
				end
				task.spawn(FadeOut)
				SlotReload1()
			end
		end)
	end
end)

In your case you put the SlotReload1() and disconnect calls outside of the entire event body, so it only got called 1 time, when the script loaded.

2 Likes

Success!
image
The script only activates once per press, and works multiple times! Thank you so much.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.