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

How to repeat a function inside itself?

Asked by 3 years ago

Keep in mind that I'm only a beginner so please try to explain each detail so I can learn from this. I needed to add something to my script, but I'm not sure how to do that exactly, the Server Script below is supposed to pick a random moderator from a table and teleport them to the caller which is the player who triggered the RemoteEvent, it works perfectly when it picks someone in-game while when it picks someone else not in-game it prints that the moderator couldn't be teleported and no moderators are teleported, I wanted to make it so if the moderator picked can't be found in-game then the function would repeat until a moderator is found in-game and gets teleported to the caller. I have asked this before but nobody answered and I managed to improve it a little myself so I decided to ask once again, below you can check what I have done so far.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teleport = ReplicatedStorage.Teleport

local function ModeratorCaller(player)
    local table = {"DukeOfWorms", "Moderator1", "Moderator2"}
    local Players = game:GetService("Players")
    local random = math.random(1, #table)
    local chosen = table[random]
    if Players:FindFirstChild(chosen) then
    game.Workspace[chosen].HumanoidRootPart.CFrame = player.Character.HumanoidRootPart.CFrame
    print("The moderator ".. Players[chosen].Name .." has been teleported to the player ".. player.Name)
    elseif not Players:FindFirstChild(chosen) then
        print("The moderator chosen couldn't be found in-game and therefore couldn't be teleported to the player " .. player.Name)
    end
end

Teleport.OnServerEvent:Connect(ModeratorCaller)
0
Using "Debounce"? Eager_Face 4 — 3y

1 answer

Log in to vote
0
Answered by
gskw 1046 Moderation Voter
3 years ago

To repeat a series of statements, we can use a loop. In your case, a repeat-until loop would be appropriate.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teleport = ReplicatedStorage.Teleport

local function ModeratorCaller(player)
    local table = {"DukeOfWorms", "Moderator1", "Moderator2"}
    local Players = game:GetService("Players")

    local playerFound = false -- Creating a new variable here, to be used later
    repeat
        local random = math.random(1, #table)
        local chosen = table[random]
        if Players:FindFirstChild(chosen) then
            game.Workspace[chosen].HumanoidRootPart.CFrame = player.Character.HumanoidRootPart.CFrame
            print("The moderator ".. Players[chosen].Name .." has been teleported to the player ".. player.Name)
            playerFound = true -- We found a player! Let's save this information in a variable.
        elseif not Players:FindFirstChild(chosen) then
            print("The moderator chosen couldn't be found in-game and therefore couldn't be teleported to the player " .. player.Name)
        end
     -- Checking the variable here.
     -- If it's false, everything between repeat and until will be executed again.
    until playerFound
end

Teleport.OnServerEvent:Connect(ModeratorCaller)

You should be mindful of that the script will teleport moderators to any player requesting it, whenever the player wants it. It all depends on what you're going for, but this might prove unpleasant to moderators.

0
I understand that and I will consider your opinion to make a change in future if necessary, but considering that the game I will be using it for will be restricted to a small community I think it won't be a problem, thank your for the concern and once again, I will consider your opinion. Thank you! DukeOfWorms 18 — 3y
Ad

Answer this question