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.
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”
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)
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