Raycasting bug? Raycasting not going where has to go

Hey guys, what’s up? I got a problem while raycasting, that is a weird problem i think, that is when i shoot my gun, the ray doesnt move with the origin part, it stays in one place i dont know why, and also when i unequip the gun and equip again, the ray doesnt go where the mouse is pointing at. I’ve never seen this problem, will it be a weld problem? thanks in advance guys.
Here i leave some proof so you guys can see my problem.
robloxapp-20200728-0903206.wmv (2.2 MB) robloxapp-20200728-0904117.wmv (3.6 MB)

if you wanna to see my code here it is.
ServerSided script

local shootEvent = game.ReplicatedStorage.Shoot
local tool = script.Parent
local shootsound = tool.ShootSound
local CurrentAmmo = tool.CurrentAmmo
local BulletsFolder = game.Workspace.balas
local debris = game:GetService("Debris")

local function bulletsTrajectory(origin, direction)
	local midpoint = origin + direction / 2 
	local part = Instance.new("Part")
	part.Anchored = true
	part.CanCollide = false
	part.BrickColor = BrickColor.new("Cool yellow")
	part.CFrame = CFrame.new(midpoint, origin)
	part.Size = Vector3.new(0.1,0.1,direction.magnitude)
	part.Parent = BulletsFolder
	debris:AddItem(part, 0.3)
end


shootEvent.OnServerEvent:Connect(function(player, originPos, destinationPos)
	local directionRay = (destinationPos - originPos).Unit * 100
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {tool}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local ResultOfRaycasting = workspace:Raycast(originPos, directionRay, raycastParams)
	if ResultOfRaycasting then
		local hitpart = ResultOfRaycasting.Instance 
		if hitpart then
			print(hitpart)
		end
	end
	bulletsTrajectory(originPos, directionRay)
end)

Client Script

local player = game.Players.LocalPlayer
local BulletsFolder = game.Workspace.balas
local tool = script.Parent.Parent
local originPosition = tool.Model:WaitForChild("Origin")
local contextactionservice = game:GetService("ContextActionService")
local ShootEvent = game.ReplicatedStorage.Shoot
local RifleGUI = tool.Model.Frame.Ammo
local CurrentAmmo = tool.Model:WaitForChild("CurrentAmmo")
local ShootSound = tool.Model:WaitForChild("ShootSound") 
local crosshair = "rbxasset://SystemCursors/Cross"
local PlayerMouse = player:GetMouse()

PlayerMouse.TargetFilter = BulletsFolder


tool.Equipped:Connect(function(mouse)
	RifleGUI.Text = CurrentAmmo.Value
	RifleGUI.Visible = true
	local originPos = originPosition.Position 
	local destinationPos = mouse.Hit.Position
	
	
	mouse.Button1Down:Connect(function()
		
		ShootEvent:FireServer(originPos, destinationPos)
		
	end)
end)

tool.Unequipped:Connect(function()
	RifleGUI.Visible = false
end)

You need to add a WaitForChild in the variable shootEvent

it didn’t work, maybe its my part that isnt welded to my gun?

I your client script also you have to use a waitforchild in ShootEvent

I added WaitForChild() and it keeps occuring the same problem, i have welded all my parts and add WaitForChild() on the remotes events.

When you unequip the tool, the mouse connection doesn’t disconnect, meaning that if you reequip the tool the previous connection is still doin things. Disconnect the function:

tool.Equipped:Connect(function(mouse)
	RifleGUI.Text = CurrentAmmo.Value
	RifleGUI.Visible = true
	local originPos = originPosition.Position 
	local destinationPos = mouse.Hit.Position
	
	local ButtonConnection -- Makes the function a variable
	ButtonConnection = mouse.Button1Down:Connect(function()
		
		ShootEvent:FireServer(originPos, destinationPos)
		
	end)
    local UnequipConnection -- Makes the function a variable
    UnequipConnection = tool.Unequipped:Connect(function()
        UnequipConnection:Disonnect() -- Disconnects the function
        ButtonConnection:Disconnect() -- Disconnects the function
	    RifleGUI.Visible = false
    end)
end)

It keeps occuring the same problem, that method didnt work aither. So what is the true problem on my rifle-gun?

Or, adding on to the previous point, you predefined the origin point variable, don’t do that, it will stay constant as long as you don’t change it.

Delete those variables.
Replace this line:

with this:

ShootEvent:FireServer(origin.Position, mouse.Hit.Position)
1 Like

Dude THANKS so much!!! you solved me my problem!!!