I am trying to make a raycast gun, and when I activate it it comes up with this error.
Here’s my code (ServerScript):
local tool = script.Parent
local character = tool.Parent
local event = tool:WaitForChild("ShootEvent")
local handle = tool:WaitForChild("Hole")
local IgnoreParts = {character, tool}
local ignorefunc = function(v)
if v:IsA("BasePart") then
if v.CanCollide == true then
table.insert(IgnoreParts,v)
end
if v.Transparency == 1 then
table.insert(IgnoreParts,v)
end
if v.Name == "Handle" then -- for Accessories
table.insert(IgnoreParts, v)
end
end
end
workspace.DescendantAdded:Connect(ignorefunc)
for i,v in pairs(workspace:GetDescendants()) do ignorefunc(v) end
event.OnServerEvent:connect(function(play, mouse)
local beam = Instance.new("Part", workspace)
local hit,Position = workspace:Raycast(handle.Position, mouse.Position * 999, IgnoreParts)
beam.Size = Vector3.new(1,1,1)
beam.Transparency = 1
beam.Anchored = true
beam.CanCollide = false
beam.CFrame = mouse
local a1 = Instance.new("Attachment", handle)
local a0 = Instance.new("Attachment", beam)
wait(0.1)
a1:Destroy()
a0:Destroy()
game:GetService("Debris"):AddItem(beam, 0.05)
end)
That was similar to how you would use the old raycasting system (albeit your ray wouldn’t end up where you wanted it with that code)
Now you need to supply a RaycastParams.
I also fixed your direction so that it ends up where you actually want it.
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstance = IgnoreParts
results = workspace:Raycast(handle.Position, (mouse.Position-handle.Position).Unit * 999, params)
if results then -- it hit something
local hit, Position = results.Instance, results.Position
-- do stuff
end
Sources found by googling “Roblox Raycasting”:
Is all of that supposed to be inside of the OnServerEvent function?
All of it should fit in the same spot as your current raycast, which was one line. Make sure that you fit the rest of the code below it inside of the if-then-end block.
This is it now, did I do it correctly?
event.OnServerEvent:connect(function(play, mouse)
local beam = Instance.new("Part", workspace)
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = IgnoreParts
local results = workspace:Raycast(hole.Position, (mouse.Position - hole.Position).unit * 999, IgnoreParts) -- it told me to consider adding local before this line
beam.Size = Vector3.new(1,1,1)
beam.Transparency = 1
beam.Anchored = true
beam.CanCollide = false
beam.CFrame = mouse
if results then
local hit, Position = results.Instance, results.Position
local a1 = Instance.new("Attachment", hole)
local a0 = Instance.new("Attachment", beam)
local trail = Instance.new("Beam", beam)
trail.Segments = 0
trail.Width1 = 0.1
trail.Width0 = 0.1
trail.FaceCamera = true
trail.Color = ColorSequence.new(Color3.new(255,255,255))
trail.Attachment0 = a0
trail.Attachment1 = a1
trail.Transparency = NumberSequence.new(0)
if hit then
if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= tool.Parent.Name then
if debounce == true then debounce = false
hit.Parent.Humanoid:TakeDamage(30)
if hit.Parent.Humanoid.Health <= 0 then
game.Players:GetPlayerFromCharacter(tool.Parent).leaderstats.GlobalKills = game.Players:GetPlayerFromCharacter(tool.Parent).leaderstats.GlobalKills + 1
end
wait(0.05)
debounce = true
end
end
end
hole.Sound:Play()
hole.Flash:Emit(1)
hole.PointLight.Enabled = true
tool.GripPos = tool.GripPos + Vector3.new(0,0,0.3)
wait(0.1)
tool.GripPos = tool.GripPos + Vector3.new(0,0,-0.3)
a1:Destroy()
a0:Destroy()
hole.PointLight.Enabled = false
game:GetService("Debris"):AddItem(beam, 0.05)
end
end)
If so, it still comes up with the same error.
No. Re-read the line for the raycast. I don’t use IgnoreParts.
Oh, it’s working now! Thank you!