Background
I am trying to make a raycast gun, and when attempting to use RaycastResult.Normal
to make a bullet hole function, I always seem to get an error and I am unsure on how I can fix it.
Scripts
LocalScript:
local tool = script.Parent
local remote = tool:WaitForChild("ShootEvent")
local Players = game:GetService("Players")
local client = Players.LocalPlayer
local cursor = client:GetMouse()
tool.Activated:Connect(function()
remote:FireServer(cursor.Hit.Position)
end)
ServerScript:
local tool = script.Parent
local shoot_part = tool:WaitForChild("Shoot")
local remote = tool:WaitForChild("ShootEvent")
local ServerStorage = game:GetService("ServerStorage")
remote.OnServerEvent:Connect(function(player, position)
if player == game.Players:GetPlayerFromCharacter(script.Parent.Parent) then
local origin = shoot_part.Position
local direction = (position - origin).Unit * 300
local params = RaycastParams.new()
params.FilterDescendantsInstances = {player.Character}
params.FilterType = Enum.RaycastFilterType.Whitelist
local result = workspace:Raycast(origin, direction, params)
local intersection = result and result.Position or origin + direction
local distance = (origin - intersection).Magnitude
local bullet_clone = ServerStorage.Bullet:Clone()
bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, - distance / 2)
bullet_clone.Parent = workspace.BulletFolder
local normal = result.Normal -- Error line
local hole_clone = ServerStorage.Hole:Clone()
hole_clone.Position = position
hole_clone.CFrame = CFrame.new(position, position + normal) -- Variable used
hole_clone.Parent = workspace.BulletFolder
if result then
local part = result.Instance
local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(10)
end
end
game.TweenService:Create(bullet_clone,TweenInfo.new(0.2,Enum.EasingStyle.Quad ,Enum.EasingDirection.Out), {Transparency = 1}):Play()
wait(0.2)
bullet_clone:Destroy()
end
end)
Images
Image of tool:
Image of ServerStorage:
Any help would be appreciated, thank you!
Credit to @sjr04 creating the helpful tutorial, you can view it here: How to make a raycasting gun