I’m making a knife that you can throw, but the raycasting is not doing what I want it to and I don’t know why. I really need help as this is the linchpin of the game and is required. What do I need to fix?
tool.KnifeEvent.OnServerEvent:Connect(function(Player, mouseCF)
--print("what")
local worldKnife = game.ReplicatedStorage.KnifeThing:Clone()
worldKnife.Mesh.TextureId = handle.Mesh.TextureId
worldKnife.CFrame = CFrame.new(handle.Position,mouseCF.Position) * CFrame.Angles(rad(-90),0,0)
handle.Transparency = 1
worldKnife.Parent = workspace
spawn(function()
rParams.FilterType = Enum.RaycastFilterType.Blacklist
rParams.FilterDescendantsInstances = {tool.Parent,worldKnife}
local knifeDestination = mouseCF.Position
local result = workspace:Raycast(worldKnife.Position,mouseCF.Position)
if result then
knifeDestination = result.Position
print("RaycastResult Returned!")
end
local beamMarker = game.ReplicatedStorage.BeamMarker:Clone()
beamMarker.Marker1.Position = worldKnife.Position
beamMarker.Marker2.Position = knifeDestination
beamMarker.Parent = worldKnife
local travelTime = (knifeDestination - worldKnife.Position).magnitude/200
local knifeTween = tweenservice:Create(worldKnife,TweenInfo.new(travelTime,Enum.EasingStyle.Linear),{Position = knifeDestination})
knifeTween:Play()
connection = worldKnife.Touched:Connect(function(hit)
print("Hit something???")
print(hit.Name)
print(hit.Parent.Name)
local isApartOfChar = hit:IsDescendantOf(tool.Parent)
isApartOfChar = tostring(isApartOfChar)
print(isApartOfChar)
if isApartOfChar == "true" then return end
if isApartOfChar == "true" then
print("WHAT ARE YOU DOING?????? KNIFE WORK!!!!!")
end
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
connection:Disconnect()
knifeTween:Cancel()
end)
knifeTween.Completed:Wait()
debris:AddItem(worldKnife,10)
end)
wait(2)
handle.Transparency = 0
end)