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

Why does the code teleport 1 player first, but 4 afterwards?

I'm trying to make a round script, that teleports four players (, who have triggered some kind of event) onto four spawns, but for some reason, it doesn't work the first time. The first player that steps on the part (in this case), gets teleported, but afterwards, it seems to work normally as intended (teleporting only if four players touched the part.) The script is a "normal" script in ServerScriptService. Also, no errors. Here is my code:

plrsteleporting = {}
game.Workspace.Spawning.Touched:Connect(function(hit) --[["Spawning" is the Part the players step on]]
    local maxplayers = game.ServerStorage.PlayersTeleporting.Value
	local char = hit.parent
	local player = game.Players:GetPlayerFromCharacter(char)
	if player then
	if maxplayers < 5 then	
	table.insert(plrsteleporting, player)
        maxplayers = maxplayers+1
	end
	local spawn1 = game.Workspace.Spawns.Spawn1
	local spawn2 = game.Workspace.Spawns.Spawn2
	local spawn3 = game.Workspace.Spawns.Spawn3
	local spawn4 = game.Workspace.Spawns.Spawn4
	local player1 = plrsteleporting[1]
	local player2 = plrsteleporting[2]
	local player3 = plrsteleporting[3]
	local player4 = plrsteleporting[4]
	
	if table.getn(plrsteleporting) == 4 then
	player1.Character:MoveTo(spawn1.Position)
	player2.Character:MoveTo(spawn2.Position)
	player3.Character:MoveTo(spawn3.Position) 
	player4.Character:MoveTo(spawn4.Position) 	
    else 
	print("There have to be 4 players.")	
end
end	
end)

(P.S. I'm a beginner at scripting so my script is really bad and inefficient. Thanks in advance!)

2 comments

0

Okie so why do you say i need to hire a scripter when you are the one that doesn't know how to script i script a lot of stuff OK

0

bruh, i literally have a problem here and you didn't even make the effort to try scripting

2 answers

I don't recommend you using :MoveTo() for this action. You can use CFrames it might help:

R6:

plrsteleporting = {}
game.Workspace.Spawning.Touched:Connect(function(hit) 
    local maxplayers = game.ServerStorage.PlayersTeleporting.Value
    local char = hit.parent
    local player = game.Players:GetPlayerFromCharacter(char)
    if player then
    if maxplayers < 5 then  
    table.insert(plrsteleporting, player)
        maxplayers = maxplayers+1
    end
    local spawn1 = game.Workspace.Spawns.Spawn1
    local spawn2 = game.Workspace.Spawns.Spawn2
    local spawn3 = game.Workspace.Spawns.Spawn3
    local spawn4 = game.Workspace.Spawns.Spawn4
    local player1 = plrsteleporting[1]
    local player2 = plrsteleporting[2]
    local player3 = plrsteleporting[3]
    local player4 = plrsteleporting[4]

    if table.getn(plrsteleporting) == 4 then
    --Start of my changes.
    player1.Character.Torso.CFrame = CFrame.new(game.Workspace.Part.CFrame.Position)--
    Change game.Workspace.Part to the place where the tp target CFrame is stored at.

    player2.Character.Torso.CFrame = CFrame.new(game.Workspace.Part.CFrame.Position)--
    Change game.Workspace.Part to the place where the tp target CFrame is stored at.

    player3.Character.Torso.CFrame = CFrame.new(game.Workspace.Part.CFrame.Position)--
    Change game.Workspace.Part to the place where the tp target CFrame is stored at.

    player4.Character.Torso.CFrame = CFrame.new(game.Workspace.Part.CFrame.Position)--
    Change game.Workspace.Part to the place where the tp target CFrame is stored at. 
    --End of my changes.
    else 
    print("There have to be 4 players.")    
end
end 
end)

R15:

plrsteleporting = {}
game.Workspace.Spawning.Touched:Connect(function(hit) 
    local maxplayers = game.ServerStorage.PlayersTeleporting.Value
    local char = hit.parent
    local player = game.Players:GetPlayerFromCharacter(char)
    if player then
    if maxplayers < 5 then  
    table.insert(plrsteleporting, player)
        maxplayers = maxplayers+1
    end
    local spawn1 = game.Workspace.Spawns.Spawn1
    local spawn2 = game.Workspace.Spawns.Spawn2
    local spawn3 = game.Workspace.Spawns.Spawn3
    local spawn4 = game.Workspace.Spawns.Spawn4
    local player1 = plrsteleporting[1]
    local player2 = plrsteleporting[2]
    local player3 = plrsteleporting[3]
    local player4 = plrsteleporting[4]

    if table.getn(plrsteleporting) == 4 then
    --Start of my changes.
    player1.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.Part.CFrame.Position)--
    Change game.Workspace.Part to the place where the tp target CFrame is stored at.

    player1.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.Part.CFrame.Position)--
    Change game.Workspace.Part to the place where the tp target CFrame is stored at.

    player1.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.Part.CFrame.Position)--
    Change game.Workspace.Part to the place where the tp target CFrame is stored at.

    player1.Character.HumanoidRootPart.CFrame = 
CFrame.new(game.Workspace.Part.CFrame.Position)  --
    Change game.Workspace.Part to the place where the tp target CFrame is stored at.
    --End of my changes. 
    else 
    print("There have to be 4 players.")    
end
end 
end)

Try that and tell me if it works, if so please upvote this comment^

1 comment

0

It didn't work :\ I got the same result.

Hello, KochbananemitGuave! I think the problem is that you used table.getn(). Instead, just put a hashtag (#) in front of the table to get how many players are in it. Also, I used an in pairs loop. The loop loops through a table. Try this:

local plrsteleporting = {}

game.Workspace.Spawning.Touched:Connect(function(hit) --[["Spawning" is the Part the players step on]]
	local maxplayers = game:GetService("ServerStorage").PlayersTeleporting.Value
	local char = hit.Parent
	local player = game:GetService("Players"):GetPlayerFromCharacter(char)
	if player then
		if maxplayers < 5 then  
			table.insert(plrsteleporting, player)
			maxplayers = maxplayers + 1
		end
		local spawn1 = game.Workspace.Spawns.Spawn1
		local spawn2 = game.Workspace.Spawns.Spawn2
		local spawn3 = game.Workspace.Spawns.Spawn3
		local spawn4 = game.Workspace.Spawns.Spawn4
	
		if #plrsteleporting == 4 then
			for i, player in pairs(plrsteleporting) do
				player.Character:MoveTo(spawn .. i)
			end
		end
	end 
end)

Also, I added :GetService() before you wrote your services as it is recommended all the time. :GetService()checks if the service is existent. If not, it creates the service. Please upvote and accept this answer if it helps.

1 comment

0

I tried your code and got the following error: "ServerScriptService.Script:19: attempt to concatenate function with number" Where is the problem?

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