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
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?
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)
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?
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 and sometimes the .RespawnLocation is nil when printed by sometimes it’s the correct SpawnLocation??
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?