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

Why are these conditional statements not working?

Asked by 9 years ago

The function "IsANPC" keeps failing to work and returns false not just that it prints twice,why?

Output:

false

false

false

false

Part has a Humanoid

Part has the value NPC

Part has a String Value

false

Part has a Humanoid

Part has the value NPC

Part has a String Value

Part has a Humanoid

Part has the value NPC

Part has a String Value

Script:

--[[Purpose:I'm making a script that is finding npcs with the value "Shop"
and making them into a shop]]--

--Part = Instance.new("Part",game.Workspace)
--Humanoid = Instance.new("Humanoid",Part)
--Value = Instance.new("StringValue",Part)
--Value.Name = "NPC"
--Value.Value = "Shop"

ShopNPCs= {}

Functions = {
    IsAShopNpc = function(Part)--Used to justify if it's a Shop Npc
        if Part:FindFirstChild("Humanoid") then
            print(Part,"has a Humanoid")
            local Value = Part:FindFirstChild("NPC")
            print(Part,"has the value NPC")
            if Value then
                if Value:IsA("StringValue") then
                    print(Part,"has a String Value")
                    if Value.Value== "Shop" then
                        print(Part,"contains the Value Shop")
                        return true
                        else return false
                    end
                end
            end
            end
    end,}

for _,Object in pairs (game.Workspace:GetChildren()) do
    if Functions.IsAShopNpc(Object) then
        table.insert(ShopNPCs,Object)
    elseif not Functions.IsAShopNpc(Object) then
        assert(false,"is not a NPC")
    end
end

If the part has all these qualities why isn't it returning true and why is it printing it twice?

How could I fix this?

1 answer

Log in to vote
1
Answered by 9 years ago

On line 21, you have if Value == "Shop" then, but this will never be true. Did you mean if Value.Name == "Shop" then ?

Minor note: you could just have a single "return false" at the end of the function IsAShopNPC (ie right after line 31) instead of having "else return false" scattered throughout the rest of the function.

0
Thanks,you helped me find the problem I mean't to put Value.Value. kevinnight45 550 — 9y
0
Why does it assert only once?Shouldn't it print twice since its on a loop? kevinnight45 550 — 9y
1
If you are referring to line 35, calling assert with a false or nil first argument will cause an error, stopping the script. You should change it to a print statement. chess123mate 5873 — 9y
Ad

Answer this question