What do I am trying to achieve? The script are supposed to move character when the character is loaded(entering the game) but it didn’t work. It only work if I reset. I want the character move to somewhere when it is loaded
What is the issue?
local Checkpoints = require(script.Checkpoints)
local DataManger = require(script.DataManager)
--this function runs when players join your game
game.Players.PlayerAdded:Connect(function(player)
local loaded = false
DataManger.AddPlayerData(player)
--this function runs when your player is added to the world, it also runs again when you reset
local character = player.Character or player.CharacterAdded:Wait()
player.CharacterAdded:Connect(function(char)
--this is called the player humanoid, it handles a lot of things like animations, jumping, walkspeed
local humanoid = char:WaitForChild("Humanoid")
--this is setting your jump power to 0, too disable player jumping!
humanoid.JumpPower = 0
humanoid.JumpHeight = 0
--this is handling for teleporting players to the checkPoints :)
if player.Data.spawnpoint.Value ~= "" then
print("Fired")
local rootPart = char:WaitForChild("HumanoidRootPart")
local foundCheckpoint = workspace.Interactions.Spawnpoints:FindFirstChild(player.Data.spawnpoint.Value)
if foundCheckpoint then
task.wait(.5)
rootPart.CFrame = (foundCheckpoint.Root.CFrame * CFrame.new(0,3,0))
else
warn("NO CHECKPOINT FOUND WITH THE NAME: "..player.Data.spawnpoint.Value.." DID YOU SET UP A CHECKPOINT WITH THAT NAME?")
end
end
end)
end)
game.Players.PlayerRemoving:Connect(DataManger.RemovePlayerData)
local character = player.Character or player.CharacterAdded:Wait() -- this line
player.CharacterAdded:Connect(function(char)
Solution: if possible try to remove that line if not needed, or replace it with local character = player.Character
other alternative that you can use which includes: outside of playeradded, make a variable called local character = nil → then define character = char when CharacterAdded event is connected
local character = nil -- or you can do player.Character
game.Players.PlayerAdded:Connect(function(player)
local loaded = false
DataManger.AddPlayerData(player)
--this function runs when your player is added to the world, it also runs again when you reset
character = player.Character -- if you want
player.CharacterAdded:Connect(function(char)
charater = char -- if you want....
--this is called the player humanoid, it handles a lot of things like animations, jumping, walkspeed
local humanoid = char:WaitForChild("Humanoid")
Explanation:
when the player spawns → this line (player.CharacterAdded:Wait()) waits until the character is defined → which means the CharacterAdded event will sometimes be missed -->, because the character already spawned in the game already and then it try to connect to CharacterAdded event but there’s no use
(CharacterAdded event is pretty sensitive because it only detects the only right at moment when the character is loaded, and will not run the content inside if it misses the event)
So there’s no need to put any sort of wait or delay before CharacterAdded event because sometimes it can be missed and cause some bug
Thank you. However, I did removed local character = player.Character or local character = player.Character or player.CharacterAdded:Wait() It didn’t really work out what I want. I’m going send a videos. Check it up.
I think I might need to take a quick look of DataManger and Checkpoints module scripts
and Btw do you mind if i ask is this a local script or a script?
it’s a bit touchy on the load up about a character beign fully loaded. Try this line after you get character and it will hold up till the right amount of time.
local humanoid = character:WaitForChild(‘Humanoid’)
local Checkpoints = require(script.Checkpoints)
local DataManger = require(script.DataManager)
--this function runs when players join your game
game.Players.PlayerAdded:Connect(function(player)
local loaded = false
DataManger.AddPlayerData(player)
--this function runs when your player is added to the world, it also runs again when you reset -- if you want
repeat task.wait() until player:WaitForChild("Data"):WaitForChild("spawnpoint") and player.Character
print("Got it")
player.CharacterAdded:Connect(function(char)
print("Got the character")
local humanoid = char:WaitForChild("Humanoid")
--this is setting your jump power to 0, too disable player jumping!
humanoid.JumpPower = 0
humanoid.JumpHeight = 0
--this is handling for teleporting players to the checkPoints :)
if player.Data.spawnpoint.Value ~= "" then
local rootPart = char:WaitForChild("HumanoidRootPart")
local foundCheckpoint = workspace.Interactions.Spawnpoints:FindFirstChild(player.Data.spawnpoint.Value)
if foundCheckpoint then
task.wait(.5)
rootPart.CFrame = (foundCheckpoint.Root.CFrame * CFrame.new(0,3,0))
else
warn("NO CHECKPOINT FOUND WITH THE NAME: "..player.Data.spawnpoint.Value.." DID YOU SET UP A CHECKPOINT WITH THAT NAME?")
end
end
end)
Checkpoints.setSavedCheckpoints(player)
end)
game.Players.PlayerRemoving:Connect(DataManger.RemovePlayerData)
Without the module
local Checkpoints = require(script.Checkpoints)
local DataManger = require(script.DataManager)
--this function runs when players join your game
game.Players.PlayerAdded:Connect(function(player)
print("Got it")
player.CharacterAdded:Connect(function(char)
print("Got the character")
end)
end)
local Checkpoints = require(script.Checkpoints)
local DataManger = require(script.DataManager)
--this function runs when players join your game
local function LoadCharacter(player)
local char = player.Character or player.CharacterAdded:Wait()
print("Got the character")
local humanoid = char:WaitForChild("Humanoid")
--this is setting your jump power to 0, too disable player jumping!
humanoid.JumpPower = 0
humanoid.JumpHeight = 0
--this is handling for teleporting players to the checkPoints :)
if player.Data.spawnpoint.Value ~= "" then
local rootPart = char:WaitForChild("HumanoidRootPart")
local foundCheckpoint = workspace.Interactions.Spawnpoints:FindFirstChild(player.Data.spawnpoint.Value)
if foundCheckpoint then
task.wait(.5)
rootPart.CFrame = (foundCheckpoint.Root.CFrame * CFrame.new(0,3,0))
else
warn("NO CHECKPOINT FOUND WITH THE NAME: "..player.Data.spawnpoint.Value.." DID YOU SET UP A CHECKPOINT WITH THAT NAME?")
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local loaded = false
DataManger.AddPlayerData(player)
--this function runs when your player is added to the world, it also runs again when you reset -- if you want
LoadCharacter(player)
player.CharacterAdded:Connect(function(char)
LoadCharacter(player)
end)
Checkpoints.setSavedCheckpoints(player)
end)
game.Players.PlayerRemoving:Connect(DataManger.RemovePlayerData)
yet again I’m still seeing this line in your function. And therefore whats the point of having
player.CharacterAdded:Wait() in a server script when you already have char provided through CharacterAdded
local char = player.Character or player.CharacterAdded:Wait()
I thought you removed it. Don’t believe me?
then use the code that you already had like this one:
game.Players.PlayerAdded:Connect(function(player)
print("Got it") -- prints here if the player added connected - it will then run the CharacterAdded event
player.CharacterAdded:Connect(function(char)
print("Got the character") -- if it prints here then you script will work completely fine - its game over - you good to go
end)
end)
and then once it prints Got the character meaning it will always run the content within that event and you will get rid of the bug. It’s just simply a confusing mistake within the structure of the script
==> the point is don’t use any sort of yielding method/ function in some of these situation when you need to capture the signal.