Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to repeat a function inside itself?

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)

1 comment

0

Using "Debounce"?

1 answer

✓ Accepted answer

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.

1 comment

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.