Math.random Problems

Hello, So I am making a hitbox with math.random featured. I don’t know if this is a problem on my end or the scripts end cause, there’s 3 different modes that I’ve made. But instead of choosing every mode it only spams one mode.

Script:

-- Key Instances --
local Folder = game:GetService('ReplicatedStorage').Remotes
local TackScript = script.Parent
local TRemote = Folder.TRemote
local RRemote = Folder.RRemote
local HitChance = math.random(1,3)
---

-- Debounce --
local Debounce = false
---

script.Parent.Parent.Torso.Touched:Connect(function(Hit)
	if Hit.Name == 'Torso' then
		-- Work Debounce --
		if not Debounce then Debounce = true
			print(HitChance)
			-- Work HitChance --
			if HitChance == 1 then
				script.Parent.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
				TRemote:FireServer('Tackle')
				RRemote:FireServer('Ragdoll')
				TackScript.Disabled = true
				wait(2)
				script.Parent.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
				RRemote:FireServer('Unragdoll')
				Debounce = false
				TackScript.Disabled = false
			--elseif HitChance ==  2 then
			--	script.Parent.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
			--	TRemote:FireServer('PBU')
			--	RRemote:FireServer('Ragdoll')
			--	TackScript.Disabled = true
			--	wait(2)
			--	script.Parent.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
			--	RRemote:FireServer('Unragdoll')
			--	Debounce = false
			--	TackScript.Disabled = false
			elseif HitChance ==  2 then
				TRemote:FireServer('Stumble')
				TackScript.Disabled = true
				wait(2)
				Debounce = false
				TackScript.Disabled = false
			elseif HitChance ==  3 then
				script.Parent.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
				TRemote:FireServer('Fumble')
				RRemote:FireServer('Ragdoll')
				TackScript.Disabled = true
				wait(2)
				script.Parent.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
				RRemote:FireServer('Unragdoll')
				Debounce = false
				TackScript.Disabled = false
			end
		end
	end
end)
---
local HitChance = math.random(1,3)

This is only performed once.

6 Likes

Because you are only randomizing the hit chance once. At the start of the script, it stays 1 or 2 or 3 forever. Fix this by inserting local HitChance = math.random(1,3) at the beginning of each event that requires a hitchance.

2 Likes

Like this?

I would put it under the debounces.

Does the math.random work now?

Yeah it does. Thank you for your response!