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

How to stop errors with if statement arguements?

Asked by 5 years ago

Hello,

I am trying to stop it from erroring when it cannot find the object "Config" but I haven't been able to figure it out.

local user = tool.Parent 
local ray = Ray.new(tool.MuzzleFlash.CFrame.p, (target - tool.MuzzleFlash.CFrame.p).unit*300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)

local TargetObj = hit and hit.Parent.Parent:FindFirstChild("Config")
local DamageObj = TargetObj
    if DamageObj then 
        DamageObj.Hull.Value = DamageObj.Hull.Value - 25
    end

Any help would be appreciated

0
try adding "if TargetObj then" and also is hit nil? the8bitdude11 358 — 5y
0
try adding "if TargetObj then" and also is hit nil? the8bitdude11 358 — 5y

1 answer

Log in to vote
1
Answered by
gullet 471 Moderation Voter
5 years ago

Line 5 is a little bit confusing hit and hit.Parent.Parent:FindFirstChild("Config") will return true or false and set that to the variable TargetObj then on the next line you're simply creating a new variable for the same value, perhaps you just want to remove hit and?

local user = tool.Parent 
local ray = Ray.new(tool.MuzzleFlash.CFrame.p, (target - tool.MuzzleFlash.CFrame.p).unit*300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)

local TargetObj = hit.Parent.Parent:FindFirstChild("Config")
if TargetObj then 
    TargetObj .Hull.Value = TargetObj .Hull.Value - 25
end
0
Ah, still didnt work. "Hull is not a member of Folder". Some of my models have the value, some dont. So ya, the errors are just annoying. thanks for trying. Decimaprism 65 — 5y
1
Ok then check that too if you want to. [If TargetObj and TargetObj:FindFirstChild("Hull") then]. gullet 471 — 5y
0
Thanks man, you did it. Days of headaches figuring this out, thanks! Decimaprism 65 — 5y
0
It wouldn't return true or false, it would return either nil or the `Config` child Vulkarin 581 — 5y
View all comments (7 more)
0
Correct but an object will evaluate as truthy whereas nil will not gullet 471 — 5y
0
Yes, but I don't know how that relates... wouldn't this script error if hit turned out to be nil? That was the whole point of the and statement in the original script Vulkarin 581 — 5y
0
No since [if nil then] is the same as [if false then] but trying to do [nil.Hull] will error, hence the check first. gullet 471 — 5y
0
TargetObj = hit.Parent.Parent -> if hit is nil then it will be incapable of indexing hit, I don't see a "if hit" statement anywhere in your script Vulkarin 581 — 5y
0
I'm not talking about Hull at all, I mean that `hit` returned from FindPartOnRay might be nil, therefore the `TargetObj` assignment will error Vulkarin 581 — 5y
0
Ah forgive me, yes that could happen and error. I didn't look at it closely when copying his code. gullet 471 — 5y
0
No worries Vulkarin 581 — 5y
Ad

Answer this question