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

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

Asked by 3 years ago

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

Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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.

0
ok IEntity_303I 80 — 3y
0
what would i write in the Decalpart.CFrame tho IEntity_303I 80 — 3y
0
CFrame.new(RaycastResult.Position, RaycastResult.Position + RaycastResult.Normal) as you did, that is the same. imKirda 4491 — 3y
0
tysm IEntity_303I 80 — 3y
View all comments (5 more)
0
but it appears behind me not on the wall IEntity_303I 80 — 3y
0
nvm i have to use ignore list IEntity_303I 80 — 3y
0
still is on my head IEntity_303I 80 — 3y
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 imKirda 4491 — 3y
0
nvm IEntity_303I 80 — 3y
Ad

Answer this question