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

Why do my if statements both pass, no matter what I do?

Asked by 3 years ago
local gui = script.Parent
local text = gui.TextBackground.Text
local op1 = gui.TextBackground.Option1
local op2 = gui.TextBackground.Option2
local headPic = gui.TextBackground.HeadPicture
local range = 7
local npcName = gui.TextBackground.NPCName
local plr = game.Players.LocalPlayer


local NPCs = {}

for i, descendant in pairs(workspace:GetDescendants()) do

    if descendant:FindFirstChild("Dialogue") then
        table.insert(NPCs, descendant)
        descendant.Head.InteractGui.Interact.Size = UDim2.new(0, 0, 0, 0)
    end
end


local closestNPC = script:WaitForChild("nil")
local previousClosestNPC = closestNPC

game:GetService("RunService").RenderStepped:Connect(function()


    previousClosestNPC = closestNPC
    closestNPC = script:WaitForChild("nil")


    local NPCsInRange = {}
    for i, NPC in pairs(NPCs) do

        local distance = plr:DistanceFromCharacter(NPC.HumanoidRootPart.Position)

        if distance <= range then
            table.insert(NPCsInRange, NPC)
        end
    end


    for i, NPCInRange in pairs(NPCsInRange) do

        if not closestNPC then closestNPC = NPCInRange end

        if closestNPC.Name == "nil" or plr:DistanceFromCharacter(closestNPC.HumanoidRootPart.Position) > plr:DistanceFromCharacter(NPCInRange.HumanoidRootPart.Position) then
            closestNPC = NPCInRange
        end
    end


    script:WaitForChild("ClosestNPC").Value = closestNPC 
end)


script:WaitForChild("ClosestNPC"):GetPropertyChangedSignal("Value"):Connect(function()

    if closestNPC.Name == "nil" then

        for i, NPC in pairs(NPCs) do

            NPC.Head.InteractGui.Interact:TweenSize(UDim2.new(0, 0, 0, 0), "InOut", "Quint", 0.3, true)
        end

        gui.TextBackground:TweenPosition(UDim2.new(0.5, 0, 1 + gui.TextBackground.Size.Y.Scale, 0), "InOut", "Quint", 0.3, true)

    else

        if previousClosestNPC and previousClosestNPC.Name ~= "nil" then previousClosestNPC.Head.InteractGui.Interact:TweenSize(UDim2.new(0, 0, 0, 0), "InOut", "Quint", 0.3, true) end

        closestNPC.Head.InteractGui.Interact:TweenSize(UDim2.new(1, 0, 1, 0), "InOut", "Quint", 0.3, true)


        previousClosestNPC = closestNPC
    end
end)


local function handleChoice(interactVal, option)

    local character = plr.Character or plr.CharacterAdded:Wait()
    local debounce = false

    if string.find(interactVal, 'H2Restaurant') and debounce == false then
        print('went thru rest')
        if option == 1 then
            debounce = true
            plr.PlayerGui.FoodShop.Frame.Visible = true 
        end


    elseif string.find(interactVal, 'H2Accessories') and debounce == false then
        print('went thru acess')
        if option == 1 then
            debounce = true
            plr.PlayerGui.AccessShop.Frame.Visible = true 
        end
    end

    gui.TextBackground:TweenPosition(UDim2.new(0.5, 0, 1 + gui.TextBackground.Size.Y.Scale, 0), "InOut", "Quint", 0.3, true)
end


local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(inp, processed)

    if processed or not closestNPC then return end

    if inp.KeyCode == Enum.KeyCode.E then

        local interactValue = closestNPC.Dialogue.Value

        local npcImage = closestNPC:FindFirstChild('ImageLabel')

        headPic.Image = npcImage.Image

        npcName.Text = closestNPC.Name

        op1.OptionText.Text = closestNPC.Option1.Value
        op2.OptionText.Text = closestNPC.Option2.Value 
        text.Text = ""
        gui.TextBackground:TweenPosition(UDim2.new(0.5, 0, 0.866, 0), "InOut", "Quint", 0.3, true)
        wait(0.3)


        op1.MouseButton1Click:Connect(function()
            handleChoice(interactValue, 1)
        end)
        op2.MouseButton1Click:Connect(function()
            handleChoice(interactValue, 2)
        end)


        for i = 1, #interactValue do

            text.Text = string.sub(interactValue, 1, i)
            wait()
        end
    end
end)

An NPC needs a option1, option2, and and dialogue. Whenever I go to a NPC, it works fine, but the moment I go to another, it gives both of their things at the same time. I really don't know what the problem is. Please help? Thank you!

Answer this question