Search code examples
luaroblox

Function Triggers even when if statement is false


local Players = game:GetService("Players")
local TouchPart = workspace["Start"] 
local target = game.Workspace.Level1


local function teleport()
    TouchPart.Touched:Connect(function(touched)
        if touched.Parent:FindFirstChild("Humanoid") then
            local Player = Players:GetPlayerFromCharacter(touched.Parent)
            if Player then
                    --Player.Character.HumanoidRootPart.CFrame = target.CFrame + Vector3.new(0, 5, 0)
                    print("Teleport")
            end
        end
    end)
end

while true do
    local zeit = game.Workspace.wall1.time.Value
    zeit = zeit - 1
    game.Workspace.wall1.time.Value = zeit
    game.Workspace.wall1.SurfaceGui.Frame.TextLabel.Text = zeit
    wait(1)
    print(zeit)
    if zeit == 0 then
        teleport()
        game.Workspace.wall1.time.Value = 20
    end
    
end

The Function Teleport should only be called if zeit == 0. And print("Teleport") should only work when zeit hits 0 and the player is touching the part.

My Problem is even if zeit isnt 0 the function teleport() prints "Teleport" into the console when a player touches the part. Am i missing something there ?


Solution

  • Your issue is that you are connecting a Touched listener every time zeit reaches zero. This listener will fire from here on out, until you disconnect it. It also means that after two loops, it will print "teleport" twice, and after three you will see it printed three times with every touch.

    You either need to disconnect the listener, or simply teleport the people touching the part when the timer strikes zero. Here's how you would do the latter :

    local Players = game:GetService("Players")
    local TouchPart = workspace["Start"] 
    local target = game.Workspace.Level1
    local zeit = game.Workspace.wall1.time
    local wallText = game.Workspace.wall1.SurfaceGui.Frame.TextLabel
    
    -- function for teleporting all of the players currently touching the TouchPart
    local function teleport()
        local touchingPlayers = {}
        local parts = TouchPart:GetTouchingParts()
        for _, touched in ipairs(parts) do
            local Player = Players:GetPlayerFromCharacter(touched.Parent)
            if Player then
                touchingPlayers[Player] = touched.Parent
            end
        end)
    
        -- teleport all the touching players
        for player, character in pairs(touchingPlayers) do
            print("Teleporting " .. player.Name)
            -- character:PivotTo(target.CFrame + Vector3.new(0, 5, 0))
        end
    end
    
    -- keep the wall text constantly updated
    zeit.Changed:Connect(function(newValue)
        wallText.Text = tostring(newValue)
    end)
    
    -- update the counter every second
    while wait(1) do
        zeit.Value -= 1
        if zeit.Value == 0 then
            teleport()
            wait(1)
            zeit.Value = 20
        end
    end