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

How to stop errors with if statement arguements?

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

2 comments

0

try adding "if TargetObj then" and also is hit nil?

0

try adding "if TargetObj then" and also is hit nil?

1 answer

✓ Accepted answer

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

11 comments

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.

1

Ok then check that too if you want to. [If TargetObj and TargetObj:FindFirstChild("Hull") then].

0

Thanks man, you did it. Days of headaches figuring this out, thanks!

0

It wouldn't return true or false, it would return either nil or the Config child

0

Correct but an object will evaluate as truthy whereas nil will not

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

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.

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

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

0

Ah forgive me, yes that could happen and error. I didn't look at it closely when copying his code.

0

No worries

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