CFrame angles with math.random not working?

Im trying to simulate recoil for my project, but the script im using isnt working for some reason:

           local randomX = math.random(-0.01,0.01)
			local randomY = math.random(0.02,0.03)
			local randomZ = math.random(-0.01,0.01)
			cc.CFrame = cc.CFrame * CFrame.Angles(randomX,randomY,randomZ)
		else
			local randomX = math.random(-0.02,0.02)
			local randomY = math.random(0.03,0.04)
			local randomZ = math.random(-0.02,0.02)
			cc.CFrame = cc.CFrame * CFrame.Angles(randomX,randomY,randomZ)

Is there another way to get around this or fix this?

1 Like

Math.random() only generates integers so if you want to get floats you can do math.random(-10,10)/100

Hope this helps!

4 Likes

Use :NextNumber() using the Random Instance, gives more random results and includes decimals, you can also use math.random() without any arguments and divide it by 10 which would give you about the exact same result.

local random = Random.new()

local ex1 = math.random()/10 -- a decimal between 0 and 1 divided by 10
local ex2 = random:NextNumber(.01, .05) -- Recommended

However since you are going for a specific Angle, you can also just convert the Angle from Degrees to Radians to make it easier to modify:

local v3 = Vector3.new(X, Y, Z) * (math.pi/180) -- This would basically
--  convert Vector3 to radians, it is the exact same as doing math.rad() to a
--  number, but this does it to all Axis instead of a single number

-- You can also use Random with its function :NextUnitVector() where
-- it will return a random direction
3 Likes

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