Server side aimable beams

I want a system where a beam will start at one point and end at your mouse position, basically an aimable beam. I also want a built-in ray casting feature where it checks for parts with CanCollide on in the way of the beam. No, I have not made an attempt at making this, I know I need help for this.

For reference and extra understanding of what I want, play Kaiju Universe and try a beam there. That’s what I want to make.

1 Like

Rule number one in the roblox dev forum or any developping community forum is to never ask people to create things for you. People are only here to help.

So I will only help.
You need a local script to detect when the player activates the beam. Use a remote event and :FireServer sending mouse position.
In the server script, with that same remote event .OnClientEvent use roblox’s raycast system, and raycast from the Player to MousePosition.
I hope that you will be able to continue from here.

Please dont ask for completed script as its against the first rule of devforum, i will instead provide with example code that you can expand and tweak yourself.

First we will need to make a Local Script, the reason being that we need to read players Mouse.Hit parameter to be able to point the beam. Inside that script we will only register a Mouse.Button1Down Event, then read Mouse.Hit and pass it to a remote event which is connected with to a Server Script which then will handle the raycasting and all logic.

LOCAL SCRIPT

local RunServcie = game:GetService("RunService")

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local RemoteEvent = game:GetService('ReplicatedStorage').UnreliableRemoteEvent

-- Send Remote event when mouse is pressed or released
local connection
Mouse.Button1Down:Connect(function()
	connection = RunServcie.Heartbeat:Connect(function()
		RemoteEvent:FireServer(Mouse.Hit.Position)
	end)
end)

Mouse.Button1Up:Connect(function()
	connection:Disconnect()
end)

Now its time for SERVER SCRIPT

local RemoteEvent = game:GetService('ReplicatedStorage'):FindFirstChild("UnreliableRemoteEvent")
local RunService = game:GetService("RunService")
local DebrisService = game:GetService("Debris")

RemoteEvent.OnServerEvent:Connect(function(Player,Mouse)
	local HumanoidRootPart = Player.Character:FindFirstChild("HumanoidRootPart")
		
	local rayOrigin = HumanoidRootPart.Position
	local Direction = HumanoidRootPart.Position - Mouse
	local RaycastResult = simpleRaycast(rayOrigin,Direction)
	local Intersection = RaycastResult and RaycastResult.Position or rayOrigin + Direction
	if RaycastResult ~= nil then -- to make sure we dont get errors when shooting at sky
-- Visuals		
local Beam = Instance.new("Part") 
		Beam.Shape = Enum.PartType.Block
		Beam.Transparency = 0.8
		Beam.Color = Color3.new(0.780392, 0.117647, 1)
		Beam.Material = Enum.Material.Neon
		Beam.Anchored = true
		Beam.CanCollide = false
		Beam.CFrame = CFrame.new(rayOrigin,Intersection) * CFrame.new(0,0,-RaycastResult.Distance/2) --Offset our beam by half of the raycast distance
		Beam.Size = Vector3.new(1,1,RaycastResult.Distance) -- Set beams size as length of our Raycast
		DebrisService:AddItem(Beam,0.05)
		Beam.Parent = workspace
	end			
end)

function simpleRaycast(rayOrigin,rayDirection)
	local rayParams = RaycastParams.new()
	rayParams.FilterType = Enum.RaycastFilterType.Exclude
	rayParams.RespectCanCollide = true
	rayParams.FilterDescendantsInstances = {} -- Put here things you want to be ignored by our raycast
	local RaycastResult = workspace:Raycast(rayOrigin,rayDirection,rayParams)

	return RaycastResult
end

I used a newly added UnreliableRemoteEvent instead of a normal remote event as we need to keep sending players mouse position to server.

ExapmleBeam.rbxl (55.3 KB)
Also here is the example so you can see how it works and play around with it. if you got any questions feel free to ask. :wink:

I looked at your file and when I tried it, the beam went the opposite direction.
Also, I didn’t know about Mouse.Button1Down until now. That could help with another thing I’m trying to do.

Yeah its not perfect but you can fix it yourself. if my answer did help you, i would appreciate if you marked it as a solution.

1 Like