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

How would i stop this script from looping too much at the same time?

Hi so i tried making a battle royale game in roblox but i have some issues heres the script. Okay so what im trying to do is try to stop my script from looping too much like when you press e near a mathmaking place u will be teleported in 10 seconds but the loop is looping too fast so it crashes the game when its teleporting players. Local script is further down

while true do
local Teleporters = {}
local TS = game:GetService("TeleportService")
local code = TS:ReserveServer(5970897194)
	game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, Input)
		if Input == "Enter" then
			Player.Character:MoveTo(workspace.TpPart2.Position)
			table.insert(Teleporters,Player)
			script.Parent.BillboardGui.Players.Text = script.Parent.BillboardGui.Players.Text.." "..#Teleporters
		for i = 10,0,-1 do
			script.Parent.BillboardGui.TextLabel.Text = i
			wait(1)
		end
			for i, v in pairs(Teleporters) do
			end
			TS:TeleportToPrivateServer(5970897194,code,Teleporters)
			table.clear(Teleporters)
		elseif Input == "Exit" then
			for i = 1, #Teleporters do
				if Teleporters[i] == Player then
					table.remove(Teleporters, i)
				end
			end
			Player.Character:MoveTo(workspace.TpPart.Position)
		end
	end)
	wait()
end
wait()

Local Script

local UIS = game:GetService("UserInputService")
local Exit = false
UIS.InputBegan:Connect(function(k,p)
	if p then return end
	if k.KeyCode == Enum.KeyCode.E then
		if Exit == false then
			if (game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position - workspace.VectorPart.Position).Magnitude then
				game.ReplicatedStorage.RemoteEvent:FireServer("Enter")
			end
		else
			game.ReplicatedStorage.RemoteEvent:FindFirstChild("Exit")
		end
	end
end)

2 comments

0

Please describe your errors, what the script is supposed to do and how you're trying to accomplish it. Right now I have no idea what is wrong with it

0

Okay so what im trying to do is try to stop my script from looping too much like when you press e near a mathmaking place u will be teleported in 10 seconds but the loop is looping too fast so it crashes the game when its teleporting players

1 answer

✓ Accepted answer

So, instead of CONSTANTLY checking if the player is inputting stuff, use UserInputService to check if they're pressing something. Also, since you want them to press on a part, you use mganitude to check if the player is near a part.

the local script

-- Localscript

local UIS = game:GetService("UserInputService")
local interactionBlock = game.Workspace.Part

local player = game.Players.LocalPlayer

UIS.InputBegan:Connect(function(input, process)
	if not process then -- Make sure player isn't in the chat
		if input.KeyCode == Enum.KeyCode.E then
			print("Player pressed E")
			
			if (player.Character.HumanoidRootPart.Position - interactionBlock.Position).Magnitude < 10 then --Make sure the player isn't too far
				game.ReplicatedStorage.TeleportEvent:FireServer()
			end
		end
	end
end)

Server script:

-- Server Script

game.ReplicatedStorage.TeleportEvent.OnServerEvent:Connect(function(player)
	-- Teleport code here.
end)

Sorry for not being able to write the server code. I don't have the time lol.

Accept if this works!

Cheers, Zander

1 comment

0

oooh u thought i meant clicking a part i actually w¨ment pressing e near a part but im glad you replied

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