SpawnLocation.Enabled not working

video:


There is a teleporter in the lobby that teleports you to a random map. In theory, when you go through the teleporter and into the map, when you die you should respawn in the map. BUT you respawn back at the lobby.

Here is the code in the map’s spawn locations:

local player = game.Players.LocalPlayer

script.Parent.Touched:Connect(function()
	script.Parent.Enabled = true
	game.Workspace.lobby.SpawnLocation.Enabled = false
end)

Is this localscript a descendant of workspace? Because if it is and it is not a descendant of a character model at the same time then the script will not run.

1 Like

Yes the localscript is located in Workspace under map > spawnlocation

Then that explain your problem, your localscript doesn’t run because it’s under workspace.

2 Likes

oh. should i switch it from a localscript to a regular script?

Well if you do, you will have to modify your system a bit since Player.LocalPlayer will return nil in a serverscript and changes made on spawnpoint will apply to everyone.
Alternatively, you can move the localscript to StarterPlayerScripts and try to reference and enable spawnpoints from there.

1 Like

image
I tried to reference and enable the spawnpoints from StarterPlayerScripts but when I die it still sends me back to the lobby.

-- tysm for helping btw
local player = game.Players.LocalPlayer
wastelandBlue = game.Workspace.wasteland.blueSpawn
wastelandRed = game.Workspace.wasteland.redSpawn
empireRed = game.Workspace.empire.redSpawn
empireBlue = game.Workspace.empire.blueSpawn

wastelandBlue.Touched:Connect(function()
	wastelandBlue.Enabled = true
	game.Workspace.lobby.SpawnLocation.Enabled = false
end)
empireBlue.Touched:Connect(function()
	empireBlue.Enabled = true
	game.Workspace.lobby.SpawnLocation.Enabled = false
end)
empireRed.Touched:Connect(function()
	empireRed.Enabled = true
	game.Workspace.lobby.SpawnLocation.Enabled = false
end)
wastelandRed.Touched:Connect(function()
	wastelandRed.Enabled = true
	game.Workspace.lobby.SpawnLocation.Enabled = false
end)

Okay I spent some time trying to make it work in a localscript but it didn’t work so I recoded it in serverscript located in ServerScriptService and managed to make it work after remembering that Player.RespawnLocation property exist:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	print("PlayerAdded")
	player.RespawnLocation = game.Workspace:WaitForChild("lobby"):WaitForChild("SpawnLocation") --Sets new player's respawnpoint to lobby.
end)
local wasteland = game.Workspace:WaitForChild("wasteland")
local empire = game.Workspace:WaitForChild("empire")
wastelandBlue = wasteland:WaitForChild("bluespawn")
wastelandRed = wasteland:WaitForChild("redspawn")
empireRed = empire:WaitForChild("redspawn")
empireBlue = empire:WaitForChild("blueSpawn")

function ChangeRespawnLocation(Location, Player)
	local Player = Players:GetPlayerFromCharacter(Player)
	if Player ~= nil then
		print("changed")
		Player.RespawnLocation = Location --Changes player's spawnpoint to given location as first parameter.
	end
end

wastelandBlue.Touched:Connect(function(hit)
	print("wastelandBlue")
	ChangeRespawnLocation(wastelandBlue, hit.Parent)
end)
empireBlue.Touched:Connect(function(hit)
	print("empireBlue")
	ChangeRespawnLocation(empireBlue, hit.Parent)
end)
empireRed.Touched:Connect(function(hit)
	print("empireRed")
	ChangeRespawnLocation(empireRed, hit.Parent)
end)
wastelandRed.Touched:Connect(function(hit)
	print("wastelandRed")
	ChangeRespawnLocation(wastelandRed, hit.Parent)
end)

1 Like