So I’ve been trying to make this gun script, but there is an error I have tried to fix, but I couldn’t fix. the error is this: 14:56:53.586 - Unable to cast RaycastResult to Ray I would really appreciate it if someone could help me find a solution
-- local script in tool
local BODY_DAMAGE = 20
local HEADSHOT = 40
local CLIP = 8
local MAX_BULLETS = 64
local SHOOTY_PART = script.Parent.Shooty
local Player = game.Players.LocalPlayer
local Character = Player.Character
script.Parent.Activated:Connect(function()
local ray = workspace:Raycast(SHOOTY_PART.Position, SHOOTY_PART.CFrame.LookVector * 50)
local hitPart = workspace:FindPartOnRay(ray)
local function visualizeRay(ray)
local part = Instance.new("Part")
part.Size = Vector3.new(1, 1, ray.Direction.Magnitude)
part.CFrame = CFrame.new(
ray.Origin + ray.Direction/2,
ray.Origin + ray.Direction
)
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
if ray then
if hitPart.Parent == Character then
if hitPart.Name == "Head" then
Character:TakeDamage(HEADSHOT)
else
Character:TakeDamage(BODY_DAMAGE)
end
end
end
end
end)
You’re mixing 2 different API versions together. workspace:Raycast returns a RaycastResult with all of the information you need to know about the raycast. Use this instead of workspace:FindPartOnRay as this will be deprecated in the near future. In this case, hitPart would be equal to ray.Instance.
Yes, your resulting code should look something like this:
script.Parent.Activated:Connect(function()
local ray = workspace:Raycast(SHOOTY_PART.Position, SHOOTY_PART.CFrame.LookVector * 50)
local hitPart = ray.Instance -- look into RaycastResult on the developer hub for more info
...
You would also need to store your origin and direction variables right before you :Raycast because ray.Origin and ray.Direction don’t exist in the new API.
local BODY_DAMAGE = 20
local HEADSHOT = 40
local CLIP = 8
local MAX_BULLETS = 64
local SHOOTY_PART = script.Parent.Shooty
local Player = game.Players.LocalPlayer
local Character = Player.Character
script.Parent.Activated:Connect(function()
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {SHOOTY_PART.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(SHOOTY_PART.Position, SHOOTY_PART.CFrame.LookVector * 50, raycastParams)
local hitPart = raycastResult.Instance
local function visualizeRay(raycastResult)
local part = Instance.new("Part")
part.Size = Vector3.new(1, 1, raycastResult.Direction.Magnitude)
part.CFrame = CFrame.new(
raycastResult.Origin + raycastResult.Direction/2,
raycastResult.Origin + raycastResult.Direction
)
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
if raycastResult then
if hitPart.Parent == Character then
if hitPart.Name == "Head" then
Character:TakeDamage(HEADSHOT)
else
Character:TakeDamage(BODY_DAMAGE)
end
end
end
end
end)
@bryancololee as well
EDIT: Everything works until the script tries to create a part
What line did you get the error?
Also, why are you raycasting in a localscript AND doing the damage in the localscript? Shouldn’t those be done in the server?
Also, character:TakeDamage() wont work, as the character doesn’t store the health property or that function. Call the function on the character’s humanoid instead.
-- local script
local BODY_DAMAGE = 20
local HEADSHOT = 40
local CLIP = 8
local MAX_BULLETS = 64
local SHOOTY_PART = script.Parent.Shooty
local Player = game.Players.LocalPlayer
local Character = Player.Character
script.Parent.Activated:Connect(function()
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {SHOOTY_PART.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(SHOOTY_PART.Position, SHOOTY_PART.CFrame.LookVector * 50, raycastParams)
script.Parent.Handle.Sound:Play()
local hitPart = raycastResult.Instance
print("Instance created")
game.ReplicatedStorage.Shooty:FireServer(Character, HEADSHOT, BODY_DAMAGE, hitPart,raycastResult )
end)
-- server script
game.ReplicatedStorage.Shooty.OnServerEvent:Connect(function(Character, HEADSHOT, BODY_DAMAGE, hitPart, raycastResult)
local function visualizeRay(raycastResult)
local part = Instance.new("Part")
part.Size = Vector3.new(1, 1, raycastResult.Direction.Magnitude)
part.CFrame = CFrame.new(
raycastResult.Origin + raycastResult.Direction/2,
raycastResult.Origin + raycastResult.Direction
)
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
print("part created")
if raycastResult then
if hitPart.Parent == Character then
if hitPart.Name == "Head" then
Character.Humanoid:TakeDamage(HEADSHOT)
print("Headshot")
else
Character.Humanoid:TakeDamage(BODY_DAMAGE)
print("Character")
end
end
end
end
end)
I think the issue is that the ray’s direction is the lookvector of SHOOTY_PART and then it will only extend 50 studs. So just to test that I’m right, make a wall in your game and point the gun towards the wall. Make sure you are below 50 studs away from the wall or the ray wont cast. To fix this I would point the rays direction at the mouse’s position, I’ll get the calculations for that since I forget them.