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

How do i fix my bullet hole script thats doesn't work for some reason?

i recently made a fps game but the bullet hole thing doesn't work. it comes with a error that says Players.IEntity_303I.Backpack.LocalScript:22: attempt to perform arithmetic (add) on nil - heres the script :

local cam = workspace.Camera
local Mouse = game.Players.LocalPlayer:GetMouse()
local Hand = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("Right Arm"):Clone()
local Model = Instance.new("Model")

Model.Name = game.Players.LocalPlayer.Name.."'s Model"
Model.Parent = workspace
Hand.Parent = Model
Hand.Name = "RightArm1"
game:GetService("RunService").RenderStepped:Connect(function()
	for i = 0,1,.0001 do
		wait()
		Hand.CFrame = Hand.CFrame:Lerp(cam.CFrame * CFrame.new(1,-1,-2), i)
	end
end)
Mouse.Button1Down:Connect(function()
	local Decalpart = Instance.new("Part")
	local hit,pos,normal = workspace:Raycast(Hand.Position,Hand.CFrame.LookVector)
	Decalpart.Transparency = 1
	Decalpart.Size = Vector3.new(1,1,1)
	Decalpart.Parent = workspace
	Decalpart.CFrame = CFrame.new(pos,pos + normal)
	local decal = "rbxassetid://73737626"
	decal.Parent = Decalpart
	decal.Face = "Front"
end)

1 answer

✓ Accepted answer

workspace:Raycast returns RaycastResult which is Instance but let's call it a table with result properties, the properties are:

  • Instance
  • Position
  • Material
  • Normal

You would simply index it like this

local hit = workspace:Raycast(Hand.Position,Hand.CFrame.LookVector)

if (hit) then
	print(hit.Normal)
	...
end

If nothing was hit, hit in your case is nil so you should check if something was hit before indexing it.

9 comments

0

ok

0

what would i write in the Decalpart.CFrame tho

0

CFrame.new(RaycastResult.Position, RaycastResult.Position + RaycastResult.Normal) as you did, that is the same.

0

tysm

0

but it appears behind me not on the wall

0

nvm i have to use ignore list

0

still is on my head

0

Raycast accepts third argument, that is RaycastParameters, you can specify FilderDescendantsInstances which are instances which's descendants will be filtered, check it out: https://developer.roblox.com/en-us/api-reference/datatype/RaycastParams

0

nvm

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