Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Help on how to make a gun with RayCasting FE?

Asked by 6 years ago

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:

01local tool = script.Parent
02local player = game:GetService("Players").LocalPlayer
03local mouse = player:GetMouse()
04local canPlay = true
05local mouseDown = false
06Equipped = false
07tool.Equipped:Connect(function()
08    Equipped = true
09    mouse.Button1Down:Connect(function()
10        mouseDown = true
11    end)
12    mouse.Button1Up:Connect(function()
13        mouseDown = false
14    end)
15    while true do
16        if mouseDown then
17            if canPlay and Equipped then
18                canPlay = false
19                local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 100)
20                local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
21                local beam = Instance.new("Part", workspace)
22                beam.BrickColor = BrickColor.new("Toothpaste")
23                beam.FormFactor = "Custom"
24                beam.Material = "Neon"
25                beam.Transparency = 0.6
26                beam.Anchored = true
27                beam.Locked = true
28                beam.CanCollide = false
29                local distance = (tool.Handle.CFrame.p - position).magnitude
30                beam.Size = Vector3.new(0.3, 0.3, distance)
31                beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
32                game:GetService("Debris"):AddItem(beam, .01)
33                if part then
34                    local humanoid = part.Parent:FindFirstChild("Humanoid")
35                    if not humanoid then
36                        humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
37                    end
38                    if humanoid then
39                        humanoid:TakeDamage(5)
40                    end
41                 end
42                 canPlay = true
43              end
44          end
45          wait(.1)
46    end
47end)
48tool.Unequipped:Connect(function()
49    Equipped = false
50end)
0
Just a tip CFrame.p is deprecated. Instead use CFrame.Position IProgram_CPlusPlus 58 — 5y

1 answer

Log in to vote
0
Answered by 6 years ago

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.

Ad

Answer this question