I am 100% sure Roblox is not acting as it should. This code wont work despite the character object being referenced correctly in workspace. The CFrame is the exact same, and the code works the first run through when the player joins. However, when the character respawns it does not work. On top of this magical issue, when the player joins if I remove the 1st characteradded the code doesn’t work at all. Im really considering quitting this platform because this is ridiculous.
game.Players.PlayerAdded:Connect(function(Player : Player)
shared.PlayerList[Player] = CoreGame.new(Player)
local ObbyObject = CoreObby.new(Player)
ObbyObject:SpawnNewPart(shared.PlayerList[Player].Data.Level)
local Char = Player.Character or Player.CharacterAdded:Wait()
Char:SetPrimaryPartCFrame(game.Workspace:WaitForChild(tostring(shared.PlayerList[Player].Data.Level)).PrimaryPart.CFrame + Vector3.new(0,5,0))
print(game.Workspace:WaitForChild(tostring(shared.PlayerList[Player].Data.Level)).PrimaryPart.CFrame)
Player.CharacterAdded:Connect(function(Char) end) -- somehow fixes a bug.
Player.CharacterAdded:Connect(function(CharO)
CharO:SetPrimaryPartCFrame(game.Workspace:WaitForChild(tostring(shared.PlayerList[Player].Data.Level)).PrimaryPart.CFrame + Vector3.new(0,5,0))
end)
end)
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player: Player)
-- Player Related Code Here
player.CharacterAdded:Connect(function(character: Model)
-- Character Related Code Here
-- do whatever you need to the character here
end
end)
Not sure why you’re getting the character before the character is added, all that’s doing is yielding until the character exists. Instead, anything you want to do the character do it inside CharacterAdded
I attempted that and it didn’t work. Thats why I am 100% certain that its not a code problem but rather Roblox. Ill try that again and let you know what happens.
I’ve had problems where the initial CharacterAdded never ran but that was around a year ago and I’ve not had a problem since. The only issue I see is that you might be yielding something when the player is added which means the CharacterAdded connection may not have connected in time.
It didn’t work, I guess I will have to find another work around to another Roblox issue. I am starting to think the higher you get the more its not actual coding but rather figuring out ways to break the engine so it works to your advantage.
local DS : DataStoreService = game:GetService("DataStoreService")
local Store : GlobalDataStore = DS:GetDataStore("Store_1123123412312")
local Module = {}
local function LoadData(Player : Player)
return Store:GetAsync(tostring(Player.UserId)) or {
Level = 1;
}
end
function Module.new(Player : Player)
return {
Player = Player;
Data = LoadData(Player);
}
end
function Module.SaveData(Data : {Player : Player, Data : {}})
Store:UpdateAsync(tostring(Data.Player.UserId), function()
return Data.Data
end)
end
function Module.AddToLevel(Data)
Data.Level += 1
end
return Module
Obby:
local Module = {}
Module.__index = Module
local CoreGame = require(game.ServerStorage.Modules.Game)
local Paths = game.ReplicatedStorage.Objects.Parts:GetChildren()
function Module.new(Player : Player)
return setmetatable({
Player = Player;
Turns = 0;
Inclines = 0;
Len = 0;
CurrentPath = game.Workspace.Jumps;
}, Module)
end
function Module:ChoosePath() : Model
local ChosenNumber : number = math.random(#Paths)
local ChosenPath : Model = Paths[ChosenNumber]:Clone()
if self.Turns >= 3 then
self.Turns = 0
self.Inclines += 1
return game.ReplicatedStorage.Objects.Conditional.Incline:Clone()
end
if self.Inclines >= 3 then
self.Inclines = 0
return game.ReplicatedStorage.Objects.Conditional.OtherTurn:Clone()
end
if ChosenPath.Name == "Turn" then
self.Turns += 1
end
return ChosenPath
end
function Module:SpawnNewPart(Level : number)
local NewPart = self:ChoosePath()
NewPart.Parent = game.Workspace
NewPart:SetPrimaryPartCFrame(self.CurrentPath:WaitForChild("End").CFrame)
NewPart.Name = Level
NewPart.End.Touched:Connect(function(Hit :BasePart)
local Data = shared.PlayerList[self.Player].Data
CoreGame.AddToLevel(Data)
self:SpawnNewPart(Data.Level)
end)
game.ReplicatedStorage.Remotes.ObbyEvent:FireAllClients(self.Player, NewPart)
local Char = self.Player.Character or self.Player.CharacterAdded:Wait()
self:RemovePath()
self.CurrentPath = NewPart
end
function Module:RemovePath()
self.CurrentPath:Destroy()
end
function Module:GetCurrentPart()
return self.CurrentPath
end
return Module
By any chance have you tried this without using these 2 lines?
local ObbyObject = CoreObby.new(Player)
ObbyObject:SpawnNewPart(shared.PlayerList[Player].Data.Level)
Just for testing and try setting the characters CFrame to something that exist. The only thing I can think of is in the :SpawnNewPart has something to do with this.
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
repeat task.wait() until Player:HasAppearanceLoaded()
-- The rest of the code here.
end)
end)
CharacterAdded only simply fires when the character model is added and not the instances inside iirc.
Yeah, that’s what I think the issue is but I can’t be sure since I don’t think it waits for the module to finish. you could move the function into the character added and send the character as an argument. Then inside the function check to see if the part exists, if it does then return else create a part.
local function onCharacterAdded(Char)
Char:SetPrimaryPartCFrame(game.Workspace:WaitForChild(tostring(shared.PlayerList[Player].Data.Level)).PrimaryPart.CFrame + Vector3.new(0,5,0))
end
game.Players.PlayerAdded:Connect(function(Player : Player)
shared.PlayerList[Player] = CoreGame.new(Player)
local ObbyObject = CoreObby.new(Player)
ObbyObject:SpawnNewPart(shared.PlayerList[Player].Data.Level)
local Char = Player.Character
if Char then onCharacterAdded(Char) end
Player.CharacterAdded:Connect(onCharacterAdded)
end)
I’m pretty sure it’s because of the default spawn system
Documentation recommends yielding
-- Wait a brief moment before teleporting, as Roblox will teleport the
-- player to their designated SpawnLocation (which we will override)
RunService.Stepped:wait()
hrp.CFrame = CFrame.new(respawnLocations[player] + Vector3.new(0, 3.5, 0))
end