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

How do i make when player touches part then timer starts only for him?

Asked by 4 years ago

How should i make when player touches part then timer starts only for same player that touches part, but when part reaches zero then teleport to location?

1 answer

Log in to vote
0
Answered by
cfiredog 274 Moderation Voter
4 years ago

Add a script to the part you want players to touch to begin the timer. Then paste the following code into that script:

local Players = game:GetService("Players")
local teleporter = script.Parent
local debounced = {}

teleporter.Touched:Connect(function(part)
    local player = Players:GetPlayerFromCharacter(part.Parent)

    if player and not debounced[player] then
        -- Prevent multiple timers from running for player
        debounced[player] = true

        -- Set the location to teleport to
        local target = Vector3.new(0,5,0)
        -- Set the timer duration
        local duration = 5

        -- Run the timer on a new thread
        spawn(function()
            -- Run the timer
            local remaining = duration
            while remaining > 0 do
                remaining = remaining - wait()
            end

            -- Timer finished, so teleport player
            player.Character.HumanoidRootPart.CFrame = CFrame.new(target)

            -- Remove the player from the table
            debounced[player] = nil
        end)
    end
end)

I have added comments to the code for you to walk through and understand. If something isn't clear, then let me know and I can explain.

0
Thank you! MatoProF 23 — 4y
Ad

Answer this question