Hi so I am trying to make a trident go to players in a list but Im having a problem
error
Unable to cast RaycastParams to Vector3
script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr, list)
local tobject = game.ReplicatedStorage.trident:Clone()
tobject.Parent = workspace
for i, player in pairs(list) do
local playerRootPart = game.Workspace:FindFirstChild(player)
print(player)
if playerRootPart then
local bodyPosition = Instance.new("BodyPosition")
bodyPosition.Parent = tobject
bodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyPosition.Position = playerRootPart.HumanoidRootPart.Position
tobject.Anchored = false
local raycastParams = RaycastParams.new()
raycastParams.IgnoreWater = true
raycastParams.FilterDescendantsInstances = {tobject}
while true do
local direction = (playerRootPart.HumanoidRootPart.Position - tobject.Position).unit
local ray = Ray.new(tobject.Position, direction * 100)
local hitPart, hitPosition, hitNormal = workspace:Raycast(ray, raycastParams)
if hitPart then
local newPosition = hitPosition - (direction * (tobject.Size.magnitude / 2))
local tweenInfo = TweenInfo.new((newPosition - tobject.Position).magnitude / 50, Enum.EasingStyle.Linear)
local tween = game:GetService("TweenService"):Create(tobject, tweenInfo, {Position = newPosition})
tween:Play()
end
wait(.1)
end
else
warn("Could not find player or object")
end
end
end)
Oh sorry I think thatās because the Raycast didnāt hit anything so the raycastResult is nil. I think you can replace the āif hitPart thenā with āif raycastResult thenā and also move the line that sets the hitPart, hitPosition, and hitNormal variables under that conditional statement.
script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr, list)
local tobject = game.ReplicatedStorage.trident:Clone()
tobject.Parent = workspace
for i, player in pairs(list) do
local playerRootPart = game.Workspace:FindFirstChild(player)
print(player)
if playerRootPart then
local bodyPosition = Instance.new("BodyPosition")
bodyPosition.Parent = tobject
bodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyPosition.Position = playerRootPart.HumanoidRootPart.Position
tobject.Anchored = false
local raycastParams = RaycastParams.new()
raycastParams.IgnoreWater = true
raycastParams.FilterDescendantsInstances = {tobject}
while true do
local direction = (playerRootPart.HumanoidRootPart.Position - tobject.Position).unit
local raycastResult = workspace:Raycast(tobject.Position, direction * 100, raycastParams)
if raycastResult then
local hitPart, hitPosition, hitNormal = raycastResult.Instance, raycastResult.Position, raycastResult.Normal
local newPosition = hitPosition - (direction * (tobject.Size.magnitude / 2))
local tweenInfo = TweenInfo.new((newPosition - tobject.Position).magnitude / 50, Enum.EasingStyle.Linear)
local tween = game:GetService("TweenService"):Create(tobject, tweenInfo, {Position = newPosition})
tween:Play()
end
wait(.1)
end
else
warn("Could not find player or object")
end
end
end)