Script not detecting when character enters the game

I am using a script that inserts a custom animation script into a character. The script is located in ServerScriptStorage.
The issue is this script only sometimes works. I placed print commands in the text and sometimes when ran, the only output is 0 and not 1 or 2, meaning that the script is not detecting the character.
Here is the script:

game.Players.PlayerAdded:connect(function(plr)
print("0)
	plr.CharacterAdded:connect(function(char)
		print("1")
		local oldanim = char:WaitForChild("Animate")
		oldanim:Destroy()
		print("2")
		--
		local newanim = script.Animate:Clone()
		newanim.Parent = char
		newanim.Disabled = false
	end)
end)
1 Like

When handling a player added or removed event, with the expectations of it running as soon as possible. Make sure to place it in a script that has no delays before hooking up the event listener.

For example, if you’re waiting on a specific part to appear in game, and then hook the event listener. The player may have already joined, thus missing the purpose of the function.

Here’s a short example in code:

game.Workspace:WaitForChild("Part")

game.Players.PlayerAdded:Connect(function(player)
   -- Some code.
end

The player may have joined before the game.Players:Connect(function(player) event listener was hooked up because we were waiting on a part.

Instead, you’d do the following to guarantee that the function is already hooked up.

game.Players.PlayerAdded:Connect(function(player)
   -- Some code.
end)

game.Workspace:WaitForChild("Part")
2 Likes

Hello, you don’t need to put quotation marks to print numbers. Is there an error in the output?

1 Like

you also forgot a quotation in print zero. Not sure if you had it coded like that or not.

1 Like

No it wasn’t in the original code, that was a typo whoops

1 Like

Oh I didn’t know that and no there isn’t any errors.

1 Like

Is there anything above this code?

1 Like

Also what you are doing is swapping out anims, Instead merely put the script in local character scripts and it will replace the animate for you aslong as both local scripts are the same name

on another note, I didn’t get any errors from your code when i add in the anim script parented under script.

game.Players.PlayerAdded:connect(function(plr)
	print(0)
		plr.CharacterAdded:connect(function(char)
			print("1")
			local oldanim = char:WaitForChild("Animate")
			oldanim:Destroy()
			print("2")
			--
			local newanim = script.Animate:Clone()
			newanim.Parent = char
			newanim.Disabled = false
		end)
end)
1 Like

Have you tried testing this in game instead of in studio only.

1 Like

Well the thing is I have two different custom characters and I am trying to give them two separate animations, it works so far its just sometimes it doesn’t go past print(0).

1 Like

Yeah I tested it in the game too and it was the same.

1 Like

No there is no other code above.

1 Like

you mention custom characters, do you think that could have something to do with it?

2 Likes

To add onto this did you disable the CharacterAutoLoads property?

3 Likes

I don’t believe it would because the custom characters are only morphed a gui button is pressed, and when testing the game, the original animation script is not being deleted even before pressing the morph button.

so like is it working right now or is it still sometimes non-functional


i get no errors and its prints right through

2 Likes

Oh my gosh I forgot I had that on, nor did I realize it would effect it but yes I did, I just turned it off and so far its working properly now, thanks for everyone that was helping me out!

Yeah it’s working now, it was the characterautoloads setting that I forgot to turn off, thanks for your help though

1 Like

I would think you would have to turn it on, not off for it to work.

on = true
off = false

Sorry apparently I cant function today, I meant that it was off and I turned it on haha

2 Likes