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

(Need Help!) Allow multiple people to be in a Touched function?

Asked by 6 years ago

So I have this script here which basically when Touched, will turn red, clone a random map from ServerStorage to a folder in the Workspace, and teleport the player to a part called "Part3". The problem is that only one person can press this button and be teleported to a map; the rest of the players have to wait until he completes the map or the timer (nuke) runs out. How could I use a delay function or something else to allow multiple people pressing the button and track the players on a table?

(A delay of about 5 seconds would probably help a lot considering players will not press a button at the exact same time)

Here is the full script:

local Teleport = "Part3"
local part = script.Parent
part.BrickColor = BrickColor.new("Bright green")


local playertable = {}


function Touch(hit)
    if hit and hit.Parent and hit.Parent:IsA("Model") then
        print("OK")
    else
        return
    end
    if part.BrickColor ~= BrickColor.new("Bright green") then
        return
    end

    part.BrickColor = BrickColor.new("Bright red")
script.Parent.BillboardGui.Enabled = false
    local mapstorage = game.Workspace:WaitForChild('nukeeasy')
    mapstorage:ClearAllChildren()

    local mapsinserverstorage = game:GetService('ServerStorage').NukeMaps.Easy:GetChildren()
    local chosenmap = mapsinserverstorage[math.random(1, #mapsinserverstorage)]


    chosenmap = chosenmap:Clone()

    chosenmap.Parent = mapstorage

    print(hit)
    print(hit.Parent)
    print(hit.Parent:IsA("Model"))
    print(chosenmap)
    print(chosenmap:findFirstChild(Teleport))

this = true
if this == true then
       wait(2) if hit and hit.Parent and hit.Parent:IsA("Model") and chosenmap and chosenmap:findFirstChild(Teleport) then

this = false
hit.Parent:moveTo(chosenmap:findFirstChild(Teleport).Position)
   wait(1)
    this = true
end

    if chosenmap.Name == "WrWaRo" then
        script.Parent.WrWaRo.Disabled = false
        end
if chosenmap.Name == "SiSe" then
    script.Parent.SiSe.Disabled = false
    end
if chosenmap.Name == "SuSo" then
script.Parent.SuSo.Disabled = false
end




    end

end

part.Touched:Connect(Touch)

Do not mind the if-then checks for chosenmap.Name, that is just another system.

Thank you!

0
Have a bool variable called "IsPlaying". Make sure when you set this to true that it comes after your debounce. You'll use this to make sure that other functions know someone is playing. xPolarium 1388 — 6y
0
If you want a table of players that are currently on the button then you could try using Region3. I don't see why you need this method though. xPolarium 1388 — 6y
0
Titles that contain "Help!" make the question a bad question immediately. User#24403 69 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

What you will need is known as a debounce. A debounce basically prevents a block of code from running over and over again. Have a variable set up for the debounce and in the code have it check if that variable is true, if true then set it to false. Then you would need to set it back to true when you need to. I'll send an example of a really basic debounce below:

local debounce = true
local function PrintStuff()
    if debounce then
        debounce = false
        print("passed debounce")
    end
end
PrintStuff()
PrintStuff()
wait(3)
debounce = true
PrintStuff()

As you can tell, it would print "passed debounce" once and then wait 3 seconds, set the variable to true and print it again.

0
Looks like I already have a debounce, named "this", but I will try to debounce the entire function though. TheLionLiar 39 — 6y
Ad

Answer this question