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

how do i change the player’s spawn when a gui is clicked?

local waitingSpawn = game.Workspace.waitingRoom.waitingSpawn
local lobbySpawn = game.Workspace.lobby.lobbySpawn
local testGui = game.StarterGui.ScreenTestGui.testGui


how would i make it that when testGui is clicked, you would spawn in lobbySpawn?

1 answer

You need a remote event. Name it TeleportToSpawn and put it in replicatedStorage.

Code:

-- Client Code (a local script inside the gui)

testGui.YourButton.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.TeleportToSpawn:FireServer()
	testGui.YourButton:Destroy()
end)


-- Server Code (a script inside ServerScriptService)

game.ReplicatedStorage.TeleportToSpawn.OnServerEvent:Connect(function()
	local char = player.Character
	local humanRP = char.HumanoidRootPart
	
	humanRP.Position = game.Workspace.Spawn.Position + Vector3.new(0,3,0)
end

If you don't understand the code, then I suggest to watch some videos on the humanoid (R6 and R15), remote functions, and the difference between client and server. I hope it helps you :)

1 comment

0

The way is correct, but another way, somewhat easier would be to set the players respawnlocation to lobbySpawn and use the LoadCharacter function. player.RespawnLocation = lobbySpawn    player:LoadCharacter()

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