Help needed with shooting a brick with raycasting

Hey ya all,
So I am making a pvp showcase for someone and I have achieved what I wanted, the only remaining thing is that I want the gun to shoot a ray I made towards the mouse:


Currently I fire a remote event to the server with the mouse position whenever the player clicks, then on the server I racycast and do the stuff to damage the player. There is no problem with the gun, works as expected. The only downside is that the ray that is shot is not visible, and so I want this small brick to get shot.
Client sided:

local mouse = game.Players.LocalPlayer:GetMouse()
script.Parent.Activated:Connect(function()
	script.Parent.Fire:FireServer(mouse.Hit.p)
end)

Server sided:

script.Parent.Fire.OnServerEvent:Connect(function(player, mouseP)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastresult = workspace:Raycast(script.Parent.Handle.Position, (mouseP - script.Parent.Handle.Position) * 300, raycastParams)
	if raycastresult then
		local hitPart = raycastresult.Instance
		local model = hitPart:FindFirstAncestorOfClass("Model")
		if model then
			if hitPart.Name == "Head" then
				local hum = model:FindFirstChildWhichIsA("Humanoid")
				hum.Health = hum.Health - 30
			elseif hitPart.Name ~= "Head" then
				local hum = model:FindFirstChildWhichIsA("Humanoid")
				hum.Health = hum.Health - 10
			end
		end
	end
end)

Thanks for help.

you need to add .unit at the end for it to be a proper direction

also, what do you mean by not visible? do you want the ray to be displayed?

It works fine for me right now, will do it later if anything occurs (also the game is just a showcase)

Yep! Just can’t figure it out. I thought I might be able to make a part and then I’d copy it from serverstorage to workspace, then it would go along the ray. This may or may not be possible, but if you have any other ways to make a ray visible, please tell me.

Your system is very vulnerable to exploits because any exploiter can just fire any position to your remote and it would register as valid considering you have no checks on the server.

Ye ik about that lol, I wanted to help my friend in his development and he told me to make a pvp game to show him how much knowledge i have so i just made this lol. what else can u expect from a game made in 1 hour.

if you want to make a simple laser, all you need is the start and end position of the ray

local function MakeLaser(Source, Point) -- Both are Vector3
    local Distance = (Point - Source).magnitude --the distance between Source and Point, in studs, so the length of the laser

    local Part = Instance.new("Part")
    Part.CanCollide = false
    Part.Anchored = true

    Part.Size = Vector3.new(0.1,0.1,Distance) --The X and Y don't matter, that's how thick your laser is, the Z is how long the laser is, hence, use Distance
    Part.CFrame = CFrame.lookAt(Source, Point) * CFrame.new(0,0,-(Distance/2))
    Part.Parent = workspace

end

not tested btw, let me know if there’s anything wrong here

1 Like

Thanks. Im having a lot of time so ill test it instantly and tell u in a minute.

for some reason its saying that “script.Parent.Pistol.Pistol.Position” is not able to pass through the function params

nvm got rid of the error but the ray isnt visible. gun still works tho. i think its cuz the part is anchored?

How are you using the function? can i see the code?

function MakeLaser(mousy) -- Both are Vector3
	local Distance = (script.Parent.Pistol.Pistol.Position - mousy).magnitude --the distance between Source and Point, in studs, so the length of the laser

	local Part = Instance.new("Part", workspace)
	Part.CanCollide = false
	Part.Anchored = true

	Part.Size = Vector3.new(0.1,0.1,Distance) --The X and Y don't matter, that's how thick your laser is, the Z is how long the laser is, hence, use Distance
	Part.CFrame = CFrame.lookAt(script.Parent.Pistol.Pistol.Position, mousy) * CFrame.new(0,0,-(Distance/2))
	Part.Parent = workspace

end

i just added what u gave me to the server sided script above. (mousy is position of the mouse, also no errors :anger:) also i got rid of the 1st argument as there was no point in it, i just replaced all its references with the real path

Can’t really see the problem from here. are u sure the function is running?

functions don’t run until you call them

local function f(x)
    print(x)
end

f(5)
1 Like

rip bruh lol i forgot to run the function :rofl: am so dum sry to waste ur time, lemme see if it runs lol

worked! thanks! ill just add a script inside the part that will destroy it after 1 second! thanks a ton!

1 Like