If Statement in lua will only work one time

I’ve tried multiple times to debug the on going error. I’ve tried use a debounce and multiple printing placements but the only thing that points out is that it activates the script multiple times.

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
local connection = nil
slot1.ReloadCounter.TextTransparency = 1
slot1.ReloadCounter.UIStroke.Transparency = 1
slot1.ReloadFrame.ImageTransparency = 1

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

which if statement is not working? what is happening and what do you want to happen?

The whole thing will only activate once, at stabsound:Play() I can only hear it once and same with the print. Everything works fine, other than that. I’ve done this post before and it went dead ;(

Your SlotReload1 function will count down from 10 no? it shouldn’t work again until then?

yes it will properly count down from 10

when you tried print debugging where did it stop? there are no errors in the log correct?

i believe so, other wise I can check again
edit: I recently updated roblox and now it won’t even work the first time without any errors. All i see is a reoccurring loadstring() error

This is a complete shot in the dark but might it be because you are overwriting the inbuilt global value “time”? This is bad practice and can cause unexpected behavior, though it might not be the cause of this specific problem, I suggest changing it to something else, like “timer” or “Time” or “debounce” or something.

2 Likes

there is a good chance that that would be the solution, but the script still fails to work in the first place, it seems like this is the code block that isnt activating right here:

		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 timer == 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)

break it into it’s parts and print each one.

local keydown = uis:IsKeyDown(Enum.KeyCode.Q)
local isMurderer = localplayer.hiddenstats.Murderer.Value
local isCharacter = touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") ~= nil
local timeZero = time == 0

print("keydown " ..  keydown and "TRUE" or "FALSE")
print("isMurderer " ..  isMurderer and "TRUE" or "FALSE")
print("isCharacter " ..  isCharacter and "TRUE" or "FALSE")
print("timeZero " ..  timeZero and "TRUE" or "FALSE")

if keydown and isMurderer and isCharacter and timeZero 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
1 Like
local keydown = uis:IsKeyDown(Enum.KeyCode.Q)
local isMurderer = localplayer.hiddenstats.Murderer.Value
local isCharacter = touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") ~= nil
local timeZero = time == 0

if keydown then
	print("isMurderer  " ..  isMurderer and "TRUE" or "FALSE")
	print("isCharacter " ..  isCharacter and "TRUE" or "FALSE")
	print("timeZero    " ..  timeZero and "TRUE" or "FALSE")

	if isMurderer and isCharacter and timeZero 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
end

This should be less spamy

1 Like

The reason it won’t run again is because you disconnect the very function you would need to ever run it again once it is running. if connection will always be true once it exists and then you set it to nil, once its nil it won’t run at all.
image

I think I’ve figured it out, essentially, because you check if time is equal to zero before playing the sound (line 51) it will only pass the if statement if you hold Q until the timer ends. My solution to this would be to just call SlotReload1 as a function at the very end of the touched event instead of using task.spawn. This way it will only set the timer to 10 and start counting when you actually hit someone.

There are 2 other problems with this script though:

As mentioned previously it is ill advised to overwrite global functions such as “time”, so I’d reccomend changing that variable name to something else.

Due to the way you handle disconnecting the touched connection it will actually continue hitting people every 10 seconds since the connection only ends directly prior to making a new one (When you press Q)

As a side note, I fear it is possible for the player to touch their own KillSphere, though wether or not that matters depends on what you do with the player once they touch the KillSphere.

2 Likes

That part of the script is correctly removing old connections, if removed the script would keep adding layers and deal double, then triple, then quadruple ad infinium damage/sounds. The highlighted section is absolutely necessary.

You are onto something, the IsKeyDown(Q) for both sections is concerning, the script should only check for keydown when starting the attack, not during hits (touched connection).

No but it permanently disables it. It’s not being stated anymore. So instead of destroying the function for no reason just add a checker that checks if its running or not and use that instead and if it is running return end.

It does not permanently disable it. connection is just a variable, it usually holds the Touched:Connect call, then when a new one needs to be made the last one is Disconnected, like any other variable assignment it is overwritten as many times as it needs to be.

Here is an example script that can be placed under a proximity prompt. When the player uses it the connection is added, if we don’t disconnect the last connections they will stack up and the player will lose tons of health by chatting instead of 10 like we want.

--!strict
local proximityPrompt: ProximityPrompt = script.Parent

local connection: RBXScriptConnection? = nil
proximityPrompt.Triggered:Connect(function(player: Player)
	print("Adding player connection for " .. player.Name)
	
	if connection then
		-- uncomment this to fix the script!
		--connection:Disconnect()
		--connection = nil
	end
	
	connection = player.Chatted:Connect(function(msg)
		print("Player said: " .. msg)
		player.Character.Humanoid.Health -= 10
	end)
end)

I will test this out hopefully this works

I have added the correct suggestions from the script, but i havent changed task.spawn, if i removed it would force the script to wait for the reload to finish before doing the fade out. The script still activates mutliple times.


-- Knife Action--
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')
		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)

		task.spawn(SlotReload1)
	end
end)

image

this was a suggestion from another coder for me to add, it could remove the ability to play the script again but i dont think that is what is happening here

1 Like