CharacterAdded event not working when defined as a table value?

Hi, for some reason, when I added this CharacterAdded event to a table, it just doesn’t run, and I don’t know why. This has never happened to me and I know that because I remember using this trick a lot for OOP things, so this is kind of odd to me. Can anyone help me? Thanks!

function PlayerManager.SetSpawnPoint(spawnpoints: {}, player)
   if PlayerManager.Players[player][“SpawnpointConnection”] then
    PlayerManager.Players[player][“SpawnpointConnection”]:Disconnect()
   end

   PlayerManager.Players[player][“Spawnpoints”] = spawnpoints

--this whole CharacterAdded event doesn’t run 
   PlayerManager.Players[player][“SpawnpointConnection”] = player.CharacterAdded:Connect(function(char)
--Also I added a print AND a wait() here when debugging and still nothing showed up
      local randomSpawn =    PlayerManager.Players[player][“Spawnpoints”][math.random(1, #PlayerManager.Players[player]["Spawnpoints"])]
      player.RespawnLocation = randomSpawn
   end)
end
1 Like

I think you’ve forgotten :Connect() after CharacterAdded.

1 Like

Oh yeah, I already had that in the actual script, I just forgot to put it in the dev forum thing so I’ll edit the post sorry. But yeah that wasn’t the cause. Do you still have any tips or anything?

1 Like

Weird i tested it for myself, and it works perfectly fine. (i dumbed it down a little bit, but logic should still be the same)

local PlayerManager = {
	Players = {}
}

local i = 1

function PlayerManager.SetSpawnPoint(spawnpoints: {}, player)
	if PlayerManager.Players[player] then
		PlayerManager.Players[player]:Disconnect()
		print("disconnected old one")
		i += 1
	end

	--this whole CharacterAdded event doesn’t run 
	PlayerManager.Players[player] = player.CharacterAdded:Connect(function(char)
		print("ran new, iteration: "..i)
	end)
end

return PlayerManager
local Players = game:GetService("Players")
local plrm = require(script.ModuleScript)

Players.PlayerAdded:Connect(function(plr: Player)
	plrm.SetSpawnPoint({}, plr)
	
	task.wait(5)
	
	plrm.SetSpawnPoint({}, plr)
	
	task.wait(5)
	
	plrm.SetSpawnPoint({}, plr)
end)
1 Like

Hmm, do the spawn points and stuff work?
Can you try setting a spawn point and then set it again after like 10 seconds to see if you spawn on the right spawn?

2 Likes

Yeah so funny enough, i just realised you don’t even need a CharacterAdded event, you can just set the respawnlocation outside of it

function PlayerManager.SetSpawnPoint(spawnpoints: {}, player)
   PlayerManager.Players[player][“Spawnpoints”] = spawnpoints

   local randomSpawn = PlayerManager.Players[player][“Spawnpoints”][math.random(1, #PlayerManager.Players[player]["Spawnpoints"])]
   player.RespawnLocation = randomSpawn
end
2 Likes

Yeah, but the thing is, this is the only way to make it so you can have a player spawn at random spawn locations!

1 Like

Are you trying to spawn the player at a random RespawnLocation every time they respawn?

1 Like

Yeah, that’s basically the trick to making random spawn locations, apparently. Also I just realized the character added thing works, and that the random spawn doesn’t return nil, BUT for some reason the player still doesn’t spawn there :frowning: and sometimes the .RespawnLocation is nil when printed by sometimes it’s the correct SpawnLocation??

1 Like

Well im assuming it has to do something with the fact that ur setting the players RespawnLocation right after they spawn, you could perhaps try it on the Died event?

2 Likes

I did some more digging, and I found out that
If set, the player will respawn at the given SpawnLocation. This property can only be set through Lua and must contain a reference to a valid SpawnLocation, which must meet the following criteria: Descendant of Workspace SpawnLocation.TeamColor is set to the Player.TeamColor or SpawnLocation.Neutral is set to true. So basically I just had the Neutral property turned off. I thought the property meant anyone could spawn on them. Whoops… thanks for all your help though @lavasance. You spent a lot of time and effort helping so thank you so much!!! Hopefully one day I can return the favor.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.