RayCasting help

Good morning, afternoon, or evening (Depending on when your reading this post) I have been having some problems with my RayCasting code. What I want to achieve with it is a server-side RayCasting for “WallBang” through materials that are allowed e.g. Plastic but that is not my problem. The problem is with my RaycastResult when I plug in my currentPosition I get the error “Unable to cast Instance to Vector3”

Server Code

Events.BulFire.OnServerEvent:Connect(function(player, bulletOrigin, direction, bulletSpeed)
	local bulletLength = 0.5  -- Distance bullet moves in one step
	local maxDistance = 100  -- Maximum range of the bullet

	local totalDistance = 0
	local currentPosition = bulletOrigin

	while totalDistance < maxDistance do
		local rayDirection = direction * bulletLength
		local raycastResult = workspace:Raycast(currentPosition, rayDirection)

		if raycastResult then
			local hitPart = raycastResult.Instance
			local hitMaterial = hitPart.Material

			if hitPart and materialWallBang(hitMaterial) then
				print("Wallbang through:", hitPart.Name)

				bulletSpeed = bulletSpeed / 2
				currentPosition = raycastResult.Position + rayDirection.Unit * 0.1  
			else
				print("Hit wall. No wallbang allowed.")
				break
			end
		else
			currentPosition += rayDirection
		end

		totalDistance += bulletLength
		wait(0.05) 
	end
end)

Client Code

	local bulletSpeed = 100
	local debounce = false

	local bullet = Instance.new("Part")
	bullet.Size = Vector3.new(0.1, 0.1, 0.1) -- Test size
	bullet.Color = Color3.fromRGB(255, 255, 0)
	bullet.CFrame = CFrame.new(game.Players.LocalPlayer.Character.Head.Position)
	bullet.Orientation = char.HumanoidRootPart.CFrame.LookVector
	bullet.Parent = workspace
	bullet.Velocity = direction * bulletSpeed
	bullet.CanCollide = true
	
	Replicated.Events.BulFire:FireServer(player, bullet.CFrame.Position, direction, bulletSpeed)

	print(direction)

	local debounce = false

	bullet.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if player and hit.Parent ~= player.Character then
				print("Target hit, health set to 0.")
				hit.Parent:FindFirstChild("Humanoid"):TakeDamage(25)
				bullet:Destroy()
			end
		elseif hit.Parent:IsA("BasePart") or hit.Parent:IsA("Model") then
			local entryPos = bullet.Position
			print("Bullet hit a part or model, destroying bullet.")
		end
	end)
end)

Any help would be appreciated, I thank you all in advance. :slight_smile:

why are u adding (player) parameter in :FireServer, u dont need to tell the server who u are, the server knows who sent the event

1 Like

Not, sure but I removed it thank you for catching it.

did it fixed ur problem? or not?

No, setting the player did not help.

Removing the player parameter I mean.

i dont get why theres still error, and what line is the error?

can u show us the new script?

–randomawesomecharacters–

It happens on local raycastResult = workspace:Raycast(currentPosition, rayDirection) with the currentPosition I tried a vector.zero and it seemed to work with no error the error was “Unable to cast Instance to Vector3”

				currentPosition = raycastResult.Position + rayDirection.Unit * 0.1  

maybe this one is causing ur currentPosition to no longer be a vector3

remove the “+ rayDirection.Unit * 0.1” and check again

also remove this

			currentPosition += rayDirection

No, I removed that line from the code but it still returned the same error I’m assuming it has to be with the raycastResult.

dont remove the whole line, just the part where u tries to add something into the raycastrasult.positon

try just

currentPosition = raycastResult.Position

I still get the same error using that code.

can we see the whole script? the script ure showing is a bit limited the client doesnt even show where direction came from

show us both scripts

local function fireBullet(direction, speed)
	local player = game.Players.LocalPlayer
	local character = player.Character or player.CharacterAdded:Wait()
	local headPosition = character:WaitForChild("Head").Position

	Events.BulFire:FireServer(headPosition, direction, speed)
end

UIS.InputBegan:Connect(function(input)
	local mouse = player:GetMouse()

	if input.KeyCode == Enum.KeyCode.Q then
		local mousePos = mouse.Hit.Position
		local x, y, z = mousePos.X, mousePos.Y, mousePos.Z

		print("Sending to server:", x, y, z)
		Replicated.Events.SerBul:FireServer(mouse.Hit.Position) -- To-do: Fire bullet fire-rate to the server to calculate bullet Velocity
	end
end)

Replicated.Events.ClientBul.OnClientEvent:Connect(function(direction)
	local bulletSpeed = 100
	local debounce = false

	local bullet = Instance.new("Part")
	bullet.Size = Vector3.new(0.1, 0.1, 0.1) -- Test size
	bullet.Color = Color3.fromRGB(255, 255, 0)
	bullet.CFrame = CFrame.new(game.Players.LocalPlayer.Character.Head.Position)
	bullet.Orientation = char.HumanoidRootPart.CFrame.LookVector
	bullet.Parent = workspace
	bullet.Velocity = direction * bulletSpeed
	bullet.CanCollide = true
	
	Replicated.Events.BulFire:FireServer(bullet.CFrame.Position, direction, bulletSpeed)

	print(direction)

	local debounce = false

	bullet.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if player and hit.Parent ~= player.Character then
				print("Target hit, health set to 0.")
				hit.Parent:FindFirstChild("Humanoid"):TakeDamage(25)
				bullet:Destroy()
			end
		elseif hit.Parent:IsA("BasePart") or hit.Parent:IsA("Model") then
			local entryPos = bullet.Position
			print("Bullet hit a part or model, destroying bullet.")
		end
	end)
end)

Server.

local function materialWallBang(material)
	local materialData = config.Materials[material.Name]
	if materialData then
		return materialData.wallBang
	end
	return false
end

Events.SerBul.OnServerEvent:Connect(function(Player, mouse)
	local x, y, z = mouse.X, mouse.Y, mouse.Z
	local character = Player.Character
	if not character then return end

	local head = character:FindFirstChild("Head")
	if not head then return end

	local direction = (Vector3.new(x, y, z) - head.Position).Unit

	Events.ClientBul:FireClient(Player, direction)
end)

Events.BulFire.OnServerEvent:Connect(function(bulletOrigin, direction, bulletSpeed)
	local bulletLength = 0.5  -- Distance bullet moves in one step
	local maxDistance = 100  -- Maximum range of the bullet

	local totalDistance = 0
	local currentPosition = bulletOrigin

	while totalDistance < maxDistance do
		local rayDirection = direction * bulletLength
		local raycastResult = workspace:Raycast(currentPosition, rayDirection)

		if raycastResult then
			local hitPart = raycastResult.Instance
			local hitMaterial = hitPart.Material

			if hitPart and materialWallBang(hitMaterial) then
				print("Wallbang through:", hitPart.Name)

				bulletSpeed = bulletSpeed / 2
				currentPosition = raycastResult.Position + rayDirection.Unit * 0.1
			else
				print("Hit wall. No wallbang allowed.")
				break
			end
		else
			currentPosition += rayDirection
		end

		totalDistance += bulletLength
		wait(0.05) 
	end
end)

i knew that was the case, i told u to remove the player parameter on BulFire:FireServer, not on BulFire.OnServerEvent

the server OnServerEvent still need the “Player” parameter, only the client BulFire:FireServer doesnt need to pass the player, because that is automatically passed already

change this

Events.BulFire.OnServerEvent:Connect(function(bulletOrigin, direction, bulletSpeed)

to this

Events.BulFire.OnServerEvent:Connect(function(Player, bulletOrigin, direction, bulletSpeed)
1 Like

Seems to work now, thank’s for your help :slightly_smiling_face:

1 Like

Im glad i was able to help, have a nice day