Help with a gun script I made

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)
1 Like

Sorry for bumping the thread. Could anyone please help me?

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.

You’re using the new raycast wrong, because workspace:Raycast already casts a ray.

What you want to do is set the parameters of the new RaycastParams, such as this:

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {caster.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

You can read more about the new ray casting here:

Good luck, please ask if you have any questions!

So I would use ray.Instance instead???

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.

Like this?

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

Apparently raycastResult.Direction doesnt exist, as you can see here:

Can’t you just replace that with SHOOTY_PART.CFrame.LookVector * 50? I believe that is also the direction, so you might be able to use that.

Edit: RaycastResult.Origin also doesn’t exist, which I also think you can substitute with SHOOTY_PART.Position?

The RaycastResult.Position returns the position of intersection, which I don’t think is what you want.

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.

Thanks for letting me know! (30 chars)

Oh so no raycastparams for the 3rd argument? Or make a origin variable?

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

@DominusInfinitus @bryancololee

Can someone please help me out?

Not sure if you mean removed, but WorldRoot:FindPartOnRay is already deprecated.

Is there something wrong with my script? Then script doesn’t make a part

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.

I already tried getting closer then 50 studs to an object, didn’t work

Does it print part created? You put print("part created") if the part was created.

no it does not print (30 charsssss)

Is there an error? Is this in a Script or a LocalScript?