My Basic Script Wont Work

I might be sleepy…

For some reason in the following script it always errors at
local Character = game.Players.ThePlayer.Character
The error says that “ThePlayer is not a valid member of Players “Players””

I even had it print the ThePlayer right before and it printed my name.

It Should work since I am manually going through the serverside and setting the the value called player my name, which is a valid member of the Players.

value = script.Parent.Parent.Player

value.Changed:Connect(function(NewValue)
wait(.5)
print(NewValue)	
	
local ThePlayer = NewValue
	
local Character = game.Players.ThePlayer.Character --ERROR HAPPENS HERE

local Humanoid = Character:WaitForChild("Humanoid")

I even tried making it this

value = script.Parent.Parent.Player

value.Changed:Connect(function(ThePlayer)
wait(.5)
print(ThePlayer)	
	
local Character = game.Players.ThePlayer.Character  --ERROR HAPPENS HERE

local Humanoid = Character:WaitForChild("Humanoid")

Still did not work

I am 100% sure the value I am entering into the StringValue is correct as I copy and pasted my NAME from the players and it even printed my username in both scripts.

So can anyone help me with this problem? It is probably something basic I am missing.
Thank you!

1 Like

.Changed doesn’t return a player which is why it can’t find the player.

1 Like

No but it returns the value, and I am setting the value to the players name so it returns the players name. (string value).

Like Stated, when I do print(ThePlayer) it prints my name out, so why does it not work on the next line when I do game.Players.ThePlayer.Character?

1 Like

So you have to use FindFirstChild.
game.Players:FindFirstChild(ThePlayer).Character

1 Like

Sorry, use this instead

game.Players:FindFirstChild(ThePlayer.Name).Character

1 Like

No it worked, Its because the .Changed function got the name of the value already. Thank you! :slight_smile:

1 Like
Value = script.Parent.Parent:WaitForChild("Player")
local Players = game:GetService("Players")

Value.Changed:Connect(function(PlayerName)
	local Player = Players:FindFirstChild(PlayerName)
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid")
end)

This issue occurred because you were trying to find a member named “Character” inside of a string value of the player’s name as opposed to the player instance itself, I’ve made some changes to make everything obvious.

2 Likes

I know this topic is solved but I just wanted to quickly mention that you should always check if the character exists before attempting to get the Humanoid, because in the event that the player’s character died and was in the process of getting a new character on respawn, or if the player left the server, the character variable would be nil, and you can’t WaitForChild on a nil variable, so it would throw an error in the output. Seeing that it’s in a connection means that the script wont necessarily break, but if it wasn’t then it would break the script.

1 Like