Raycast returning nil. even if clearly aiming at something

the raycast only returns not nil when aiming at the sky. which is supposed to be opposite.

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local GunFunction = ReplicatedStorage.Functions.GunFunction
local BulletEvent = ReplicatedStorage.Events.BulletEvent
local Players = game:GetService('Players')
GunFunction.OnServerInvoke = function(Player,MousePosition)
	local Character = Player.Character
	if Character then
		for _,Value in pairs(Character:GetChildren()) do
			if Value:IsA('Tool') then
				local Ammo,Reloading,Debounce,GunName,DebounceTime,Muzzle,Length = Value:GetAttribute('Ammo'),Value:GetAttribute('Reloading'),Value:GetAttribute('Debounce'),Value:GetAttribute('GunName'),Value:GetAttribute('DebounceTime'),Value:FindFirstChild('Muzzle'),Value:GetAttribute('Length')
				if Ammo and Debounce and GunName and Muzzle and DebounceTime and Ammo >= 1 and not Reloading and os.clock() >= Debounce then
					if GunName == 'Revolver' then
						Value:SetAttribute('Ammo',Ammo-1)
						Value:SetAttribute('Debounce',os.clock()+DebounceTime)
						local Origin = Muzzle.Position
						local Direction = (Origin-MousePosition)*Length
						local Params = RaycastParams.new()
						Params:AddToFilter(Character)
						for _,Value in pairs(Players:GetPlayers()) do
							if Value ~= Player then
								Character = Player.Character
								if Character then
									for _,Value in pairs(Character:GetChildren()) do
										if Value:IsA('Accessory') or Value:IsA('Tool') then
											Params:AddToFilter(Value)
										end
									end
								end
							end
						end
						Params.FilterType = Enum.RaycastFilterType.Exclude
						local Raycast = workspace:Raycast(Origin,Direction,Params)
						if Raycast then
							local Model = Raycast.Instance:FindFirstAncestorOfClass('Model')
							local Humanoid = nil
							if Model then
								Humanoid = Model:FindFirstChild('Humanoid')
								if Humanoid then
									if Raycast.Instance.Name == 'Head' then
										Humanoid:TakeDamage(50)
									else
										Humanoid:TakeDamage(25)
									end
								end
							end
							if Humanoid then
								BulletEvent:FireAllClients(true,Length,Muzzle,Raycast.Position,DebounceTime)
							else
								BulletEvent:FireAllClients(false,Length,Muzzle,Raycast.Position,DebounceTime)
							end
						else
							BulletEvent:FireAllClients(false,Length,Muzzle,MousePosition,DebounceTime)
						end
					end
					break
				end
			end
		end
	end
	return MousePosition
end

can someone fix it? thanks.

1 Like

What is MousePosition here?

I’m guessing you have your direction mixed up, direction = goal - origin whereas it looks like you have origin - goal. It might also have to do with the length of the direction, you probably want to normalize it (ie (goal - origin).Unit * length

1 Like

Its the players mouse position in 3D
edit: thank u i reversed it and it worked

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.