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

only teleport if 2 players are touching?

Asked by 2 years ago

Hello guys,

Ive compiled a script to make it so it teleports a player to a random location in the two coordinates, but I want it so it only teleports if 2 players are touching it at once. I have no idea how to go about this and wondered if anyone could help?

Heres the script:

local teleportpart = script.Parent 
    teleportpart.Touched:Connect(function(hit)
        if hit.Parent.HumanoidRootPart then
            hit.Parent.HumanoidRootPart.Position = Vector3.new(math.random(-20,-5),0,math.random(23,45))
        end
    end) 

Thanks

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
2 years ago

Just wrote a little code to do what you ask. Though its tested with more than 2 players it will only teleport 2 players at a time. I haven't tested it for projectiles or other things that are not players. But it should be ok.

Code: (Added comments explaining what does what, hope its good enough! lol)

local teleportpart = script.Parent -- Our teleport part
local Player1 = nil -- Player 1
local Player2 = nil -- Player 2

local LastPlayerHit = nil -- Last Player that hit our teleport
local Disabled = false -- This works just like the Debounce trigger in disabling the teleport just after teleportation of the two players.


-- Note you might need a timer to reset the teleport after only one player has touched it otherwise the first player could be half way across the map and when another player touched the teleport part they both get teleported. Unless this is what you was going for!? lol

teleportpart.Touched:Connect(function(hit) -- our Touch event
    local Char = hit.Parent -- The possible Character
    local IsPlayer = (function(c) -- inline Function that returns if we're dealing with a player or not
        local bisPlayer = game.Players:GetPlayerFromCharacter(c)
        local Human = c:FindFirstChild("Humanoid")
        return (bisPlayer ~= nil and Human ~= nil)
    end)(Char)

    if(Disabled == false) then -- make sure we're not teleporting other players before checking stuff
        -- check to see if the touching player hasn't touched us before, if they havent then record them in memory!
        if(Char ~= LastPlayerHit and Char ~= Player1 and Char ~= Player2 and IsPlayer == true) then
            LastPlayerHit = hit.Parent -- Update the last player that touched us, this works like a kinda debounce system!
            if(Player1 == nil) then -- if we havent got a player1 then record them!
                Player1 = Char
            elseif(Player2 == nil) then -- if we havent got a player2 then record them here!
                Player2 = Char
            end
        end
        --print(Player1,Player2)
        if(Player1 ~= nil and Player2 ~= nil) then -- if we have both player slots filled then lets see if they are valid and teleport them! 
            local Player1_HRP = Player1:FindFirstChild("HumanoidRootPart")
            local Player2_HRP = Player2:FindFirstChild("HumanoidRootPart")

            if(Player1_HRP ~= nil and Player2_HRP ~= nil) then
                -- both players check out ok lets teleport them both!...    
                Player1_HRP.Position = Vector3.new(math.random(-20,-5),0,math.random(23,45))
                Player2_HRP.Position = Vector3.new(math.random(-20,-5),0,math.random(23,45))
                -- Reset our memory back to nil for the next teleport   
                Player1 = nil
                Player2 = nil
                LastPlayerHit = nil
                Disabled = true
                -- disable us and wait for the players to teleport then re-enable us
                wait(1)
                Disabled = false
            else -- something went wrong lets just reset! 
                Player1 = nil
                Player2 = nil
                LastPlayerHit = nil
                Disabled = true
                wait()
                Disabled = false        
            end
        end
    end
end) 
--[[ Not needed just here for debugging!
while true do
    print(Player1,Player2,LastPlayerHit)
    wait()
end
]]

Hope this helps! :)

0
Life Saver! By timer do you mean wait()? Helloimpets 17 — 2y
0
kinda. i was thinking of a cool down that you could make when only one player touched the teleport to reset it. Lets say only 1 player touched it then walks away, For about 5 mins no-one else touches the teleport. I think the teleport should have a reset so then the next player or two count. And the tport has forgotten the first player. TGazza 1336 — 2y
Ad

Answer this question