Math.random() and boolean value not being called during a touch event?

Hey fellow devs! I ran into an issue while scripting.

Either the math.random or the boolean value event is not being called when the touch event occurs. I need help to try to fix this script please.

Here is the portion of it that I need help with:

elseif workspace.truck.Value == true then
			if OtherPlayerChar:FindFirstChild("Football") then return end
			local randomnumber = math.random(1,3)
			print(randomnumber)
			local debounce3 = false
			if not debounce3 then
				debounce3 = true
				print("Trucked")
				workspace.HBPH:Play()
				workspace.HPH:Play()
					
				-- Dummy Movement
				
				if workspace.hitstick.Value == true then					
					if randomnumber == 1 then
						print("Random = 1!")
						OtherPlayerChar.Humanoid.PlatformStand = true

						if (not Player.Character:FindFirstChild("PushForce")) and (not OtherPlayerChar:FindFirstChild("PushForce")) then
							local PushForce = Instance.new("BodyVelocity")
							PushForce.Name = "PushForce"
							PushForce.MaxForce = Vector3.new(100000000, 100000000, 100000000)
							PushForce.Velocity = (-OtherPlayerChar.HumanoidRootPart.CFrame.LookVector) * 20
							PushForce.Parent = OtherPlayerChar.HumanoidRootPart

							wait(0.25)
							PushForce:Destroy()
						end

						wait(1)
						OtherPlayerChar.Humanoid.PlatformStand = false
						
					elseif randomnumber == 2 then
						print("Random = 2!")
						Player.Character.Humanoid.PlatformStand = true

						if (not Player.Character:FindFirstChild("PushForce")) and (not OtherPlayerChar:FindFirstChild("PushForce")) then
							local PushForce = Instance.new("BodyVelocity")
							PushForce.Name = "PushForce"
							PushForce.MaxForce = Vector3.new(100000000, 100000000, 100000000)
							PushForce.Velocity = (-Player.Character.HumanoidRootPart.CFrame.LookVector) * 20
							PushForce.Parent = Player.Character.HumanoidRootPart

							wait(0.25)
							PushForce:Destroy()
						end

						wait(1)
						Player.Character.Humanoid.PlatformStand = false
						
					elseif randomnumber == 3 then
						print("Random = 2!")
						Player.Character.Humanoid.PlatformStand = true
						OtherPlayerChar.Humanoid.PlatformStand = true

						if (not Player.Character:FindFirstChild("PushForce")) and (not OtherPlayerChar:FindFirstChild("PushForce")) then
							local PushForce = Instance.new("BodyVelocity")
							PushForce.Name = "PushForce"
							PushForce.MaxForce = Vector3.new(100000000, 100000000, 100000000)
							PushForce.Velocity = (-Player.Character.HumanoidRootPart.CFrame.LookVector) * 20
							PushForce.Parent = Player.Character.HumanoidRootPart
							
							local PushForce2 = Instance.new("BodyVelocity")
							PushForce.Name = "PushForce"
							PushForce.MaxForce = Vector3.new(100000000, 100000000, 100000000)
							PushForce.Velocity = (-OtherPlayerChar.HumanoidRootPart.CFrame.LookVector) * 20
							PushForce.Parent = OtherPlayerChar.HumanoidRootPart
							
							wait(0.25)
							PushForce:Destroy()
						end

						wait(1)
						Player.Character.Humanoid.PlatformStand = false
					end
				end
				
				OtherPlayerChar.Humanoid.PlatformStand = true
				wait(1)
				OtherPlayerChar.Humanoid.PlatformStand = false

				
				wait(1)
				debounce3 = false
			end
		end
	end
end)

The full script:

local Player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)


script.Parent.Touched:Connect(function(Part)
	local OtherPlayer = game.Players:GetPlayerFromCharacter(Part.Parent)
	local OtherPlayerChar = Part.Parent
	local Jersey = Part
	
	if Jersey.Name == "Jersey2" then
		
		if workspace.hitstick.Value == true then
			local debounce = false
			if not debounce then
				debounce = true
				print("Hit")
				workspace.HBPH:Play()
				workspace.HPH:Play()
				
				-- Dummy Movement
				
				Jersey.Position = Jersey.Position - Vector3.new(0,0,1)
				Jersey.Orientation = Vector3.new(-30, 0, 0)
				if math.random(1,5) == 3 then
					Jersey.Anchored = false
					print("Broke Off!")
				end
				wait(0.75)
				if Jersey.Anchored == false then return end
				Jersey.Position = Jersey.Position + Vector3.new(0,0,1)
				Jersey.Orientation = Vector3.new(0, 0, 0)
				wait(5)
				debounce = false
			end
			elseif workspace.block.Value == true then
			local debounce2 = false
			if not debounce2 then
				debounce2 = true
				print("Blocked")
				workspace.HBPH:Play()

				-- Dummy Movement

				Jersey.Position = Jersey.Position - Vector3.new(0,0,1)
				Jersey.Orientation = Vector3.new(-30, 0, 0)
				wait(0.75)
				Jersey.Position = Jersey.Position + Vector3.new(0,0,1)
				Jersey.Orientation = Vector3.new(0, 0, 0)
				wait(1)
				debounce2 = false
			end
		end

		-- REAL PLAYERS
		
	elseif Jersey.Name == "Jersey" then
		if workspace.hitstick.Value == true then
			if workspace.truck.Value == true then return end
			if not OtherPlayerChar:FindFirstChild("Football") then return end
			local debounce = false
			if not debounce then
				debounce = true
				print("Hit")
				workspace.HBPH:Play()
				workspace.HPH:Play()

				-- Player Movement

				OtherPlayerChar.Humanoid.PlatformStand = true
				
				
				if (not Player.Character:FindFirstChild("PushForce")) and (not OtherPlayerChar:FindFirstChild("PushForce")) then
					local PushForce = Instance.new("BodyVelocity")
					PushForce.Name = "PushForce"
					PushForce.MaxForce = Vector3.new(100000000, 100000000, 100000000)
					PushForce.Velocity = (-OtherPlayerChar.HumanoidRootPart.CFrame.LookVector) * 20
					PushForce.Parent = OtherPlayerChar.HumanoidRootPart
					
					wait(0.25)
					PushForce:Destroy()
				end
				
				wait(1)
				
				OtherPlayerChar.Humanoid.PlatformStand = false
				
				wait(5)
				debounce = false
			end
		elseif workspace.block.Value == true then
			local debounce2 = false
			if not debounce2 then
				debounce2 = true
				print("Blocked")
				
				-- Dummy Movement
				
				if (not Player.Character:FindFirstChild("PushForce")) and (not OtherPlayerChar:FindFirstChild("PushForce")) then
					local PushForce = Instance.new("BodyVelocity")
					PushForce.Name = "PushForce"
					PushForce.MaxForce = Vector3.new(100000000, 100000000, 100000000)
					PushForce.Velocity = (-OtherPlayerChar.HumanoidRootPart.CFrame.LookVector) * 9.5
					PushForce.Parent = OtherPlayerChar.HumanoidRootPart

					wait(0.25)
					PushForce:Destroy()
				end
				
				wait(1)
				debounce2 = false
			end
		elseif workspace.truck.Value == true then
			if OtherPlayerChar:FindFirstChild("Football") then return end
			local randomnumber = math.random(1,3)
			print(randomnumber)
			local debounce3 = false
			if not debounce3 then
				debounce3 = true
				print("Trucked")
				workspace.HBPH:Play()
				workspace.HPH:Play()
					
				-- Dummy Movement
				
				if workspace.hitstick.Value == true then					
					if randomnumber == 1 then
						print("Random = 1!")
						OtherPlayerChar.Humanoid.PlatformStand = true

						if (not Player.Character:FindFirstChild("PushForce")) and (not OtherPlayerChar:FindFirstChild("PushForce")) then
							local PushForce = Instance.new("BodyVelocity")
							PushForce.Name = "PushForce"
							PushForce.MaxForce = Vector3.new(100000000, 100000000, 100000000)
							PushForce.Velocity = (-OtherPlayerChar.HumanoidRootPart.CFrame.LookVector) * 20
							PushForce.Parent = OtherPlayerChar.HumanoidRootPart

							wait(0.25)
							PushForce:Destroy()
						end

						wait(1)
						OtherPlayerChar.Humanoid.PlatformStand = false
						
					elseif randomnumber == 2 then
						print("Random = 2!")
						Player.Character.Humanoid.PlatformStand = true

						if (not Player.Character:FindFirstChild("PushForce")) and (not OtherPlayerChar:FindFirstChild("PushForce")) then
							local PushForce = Instance.new("BodyVelocity")
							PushForce.Name = "PushForce"
							PushForce.MaxForce = Vector3.new(100000000, 100000000, 100000000)
							PushForce.Velocity = (-Player.Character.HumanoidRootPart.CFrame.LookVector) * 20
							PushForce.Parent = Player.Character.HumanoidRootPart

							wait(0.25)
							PushForce:Destroy()
						end

						wait(1)
						Player.Character.Humanoid.PlatformStand = false
						
					elseif randomnumber == 3 then
						print("Random = 2!")
						Player.Character.Humanoid.PlatformStand = true
						OtherPlayerChar.Humanoid.PlatformStand = true

						if (not Player.Character:FindFirstChild("PushForce")) and (not OtherPlayerChar:FindFirstChild("PushForce")) then
							local PushForce = Instance.new("BodyVelocity")
							PushForce.Name = "PushForce"
							PushForce.MaxForce = Vector3.new(100000000, 100000000, 100000000)
							PushForce.Velocity = (-Player.Character.HumanoidRootPart.CFrame.LookVector) * 20
							PushForce.Parent = Player.Character.HumanoidRootPart
							
							local PushForce2 = Instance.new("BodyVelocity")
							PushForce.Name = "PushForce"
							PushForce.MaxForce = Vector3.new(100000000, 100000000, 100000000)
							PushForce.Velocity = (-OtherPlayerChar.HumanoidRootPart.CFrame.LookVector) * 20
							PushForce.Parent = OtherPlayerChar.HumanoidRootPart
							
							wait(0.25)
							PushForce:Destroy()
						end

						wait(1)
						Player.Character.Humanoid.PlatformStand = false
					end
				end
				
				OtherPlayerChar.Humanoid.PlatformStand = true
				wait(1)
				OtherPlayerChar.Humanoid.PlatformStand = false

				
				wait(1)
				debounce3 = false
			end
		end
	end
end)
1 Like

Debugging tip: Use print calls to trace which conditions are being tracked

local a = 1
local b = 2

if a == 1 then
  print("beep", b) --> beep   2 (this will reach)
  if b == 1 then
    print("boop") --> (uh oh, this didn't print, lets find out why)
  end
end

I cant figure out what’s going on here though, so i’ll just leave you with that.

1 Like

elseif workspace:WaitForChild("truck").Value then

== true in conditional statements isn’t needed.

1 Like

I don’t know either, but all of your debounces are not really doing anything, and I can’t tell what you are debouncing…

1 Like

Thank you all for your replies, @Limited_Unique , @BJCarpenter , @metatablecatmaid . So basically, what i have here is a test script for a football game im working on.

When you press G, it fires a remote event, which makes a boolean value named “trucked” in workspace turn to true for 0.75 seconds.

This is a touch event script, if you look at the full script. The other players have a jersey welded to their humanoid root part. So when you touch that, it checks if the “trucked” value in workspace is true. If it is set to true, then it proceeds with the following:

Check to see if the other player has a football tool (normally found in the character), which if it doesnt, then it skips all that line of code in the “elseif” function.

It then creates a random number (you will see why in the next couple of lines) from 1 to 3, and prints the number chosen to make sure it was picked correctly (I agree i have too many debounces for nothing, I need to fix that.).

It plays a hit sound, and checks to see if the “hitstick” boolean value in workspace is true (which is activated when you press r for 0.75 seconds, otherwise it just proceeds normally and the other player’s humanoid.PlatformStand is set to true, then after 1 second set to false.)

Now here comes the random number part. If the “hitstick” value in workspace is set to true while the collision/touch event occurs and the truck value is true, i want it to generate a random number. If the random number is equal to 1, then i want the other player to get trucked (platformstand set to true along with body velocity generation as shown in the script.)

Otherwise, if the random number is equal to 2, I want the local player to get trucked (local player’s platformstand set to true along with body velocity as shown in the script).

Lastly, if the random number is equal to 3, I want both the players to get trucked (both player’s platformstand set to true along with body velocity as shown in the script).

In the last couple of lines of the script, this is for if hitstick is not equal to true.

Thank you all for reading this, i hope this cleared up most confusion, I am sorry this is a lot to read. Do you think you can help me try to fix the issue? I will describe the issue below.

Okay, so my main issue here is that after the touched event, once the truck value is set to true, the math.random() works fine. It’s just that it is not detecting if the hitstick value is set to true, and that is where the script doesn’t work. Again, I hope I cleared up some confusion. If you have any questions, feel free to ask away. Thank you once again!