Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Help on how to make a gun with RayCasting FE?

Hi! I made a Laser Gun using Roblox's Tutorial and I want to make it FE compatible. Any help on how I would go about it? Local Script:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local canPlay = true
local mouseDown = false
Equipped = false
tool.Equipped:Connect(function()
    Equipped = true
    mouse.Button1Down:Connect(function()
	    mouseDown = true
    end)
    mouse.Button1Up:Connect(function()
	    mouseDown = false
    end)
    while true do
	    if mouseDown then
		    if canPlay and Equipped then
			    canPlay = false
			    local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 100)
			    local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
			    local beam = Instance.new("Part", workspace)
			    beam.BrickColor = BrickColor.new("Toothpaste")
			    beam.FormFactor = "Custom"
			    beam.Material = "Neon"
			    beam.Transparency = 0.6
			    beam.Anchored = true
			    beam.Locked = true
			    beam.CanCollide = false
			    local distance = (tool.Handle.CFrame.p - position).magnitude
			    beam.Size = Vector3.new(0.3, 0.3, distance)
			    beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
			    game:GetService("Debris"):AddItem(beam, .01)
			    if part then
				    local humanoid = part.Parent:FindFirstChild("Humanoid")
				    if not humanoid then
					    humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
				    end
				    if humanoid then
					    humanoid:TakeDamage(5)
				    end
			     end
			     canPlay = true
		      end
		  end
		  wait(.1)
	end
end)
tool.Unequipped:Connect(function()
	Equipped = false
end)

1 comment

0

Just a tip CFrame.p is deprecated. Instead use CFrame.Position

1 answer

Fire an event which talks to a server script inside the tool and which then the server script does the raycasting. Raycasting or any other modification to existing parts or making new ones cannot be done with LocalScripts because of FilteringEnabled.

This discussion is preserved as it was. New answers, comments, and votes are closed.