Need help with Raycasting

Here is my code:

local RS = game:GetService("ReplicatedStorage")
local Bullet = RS:WaitForChild("Bullet")

local Turret = script.Parent

local fireRate = 0.5
local BulletDamage = 10
local BulletSpeed = 500
local agroDist = 100

while wait(fireRate) do
	
	--Find the target, detect if realistic to shoot
	local target = nil
	for i,v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local Torso = v:FindFirstChild("Torso")
		if human and Torso and human.Health > 0 then
			if (Torso.Position - Turret.Position).Magnitude < agroDist then
				target = Torso
			end
		end
	end
	if target then
		--turn the turret to target
		local Torso = target
		Turret.CFrame = CFrame.new(Turret.Position, Torso.Position )
		
		local newBullet = Bullet:Clone()
		newBullet.Position = Turret.Position
		newBullet.Parent = game.Workspace
		newBullet.Velocity = Turret.CFrame.LookVector + BulletSpeed
		
		newBullet.Touched:Connect(function(objectHit)
			local human = objectHit.Parent:FindFirstChild("Humanoid")
			if human then
				human:TakeDamage(BulletDamage)
			end
		end)
	end
end

here is the error:
v

Any help is appreciated :slightly_smiling_face:

You have to multiply it by the bullet speed, like this:

newBullet.Velocity = Turret.CFrame.LookVector * BulletSpeed

how dare you lie, there is no ray casting

this is raycasting according to devking

Thank you it worked now :slightly_smiling_face: :slightly_smiling_face: :slightly_smiling_face:

your definetley not a developer. go and study programming before accusing someone of lying

2 Likes

This is useless to the question and to the forum.
Also he’s right, there is no ray casting going on in this script.
The guy was making a joke and you have to be so rude?
Please read the forum rules.

i misinterpreted what he was trying to say. i thought that he was saying that you can’t raycast in roblox.