The character won't move when it enter the game

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)
3 Likes

Please help me out… Anyone? I need a help so badly. I’m beginner at programming stuff.

2 Likes

So what im seeing here is this line

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

1 Like

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.

https://gyazo.com/1f7090052633b36fb9a8207c7c222b82

https://gyazo.com/e5014766b084af5ae04a134850be7d9c

1 Like

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?

1 Like

It’s server script in ServerScriptService

1 Like

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’)

1 Like

Tried that too. I really need help with this :sob:

1 Like

What are you talking about my posted script works fine. Even tells you where to put it.
Copy paste done … Took that picture after a few deaths.

1 Like

Seem like when I do it without the modules, it work fine. If I use the module. It skipped the characteradded.

1 Like

My script with modules

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)

Results: https://gyazo.com/03e45c8354ec02f7bc581ffb312441b3 (With Module)

Results: https://gyazo.com/155bcc4474ac13476a33c480a8ecd590 (Without Module)

My best solution so far is this

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)

This line here is a goto line for me.

1 Like

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.

CharacterAdded may as well be CharacterStartedToBeAddedButItIsKindOfThere

Topic is now solved so I’m done.

1 Like

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