Can I define a character with Connect?

local Character = player.Character or player.CharacterAdded:Connect()
1 Like

What are you trying to achieve with this script?

There isn’t really a goal behind this, just wondering if it’s possible to use in general

local Character = player.Character is possible, you’d just have to define the player variable before.

player.Character:Connect() is not possible, as player.Character is not a valid event

1 Like

Well you’re just defining a variable/making a reference, so it’s not like it would do anything. You would probably get an error with the second part since :Connect is for connection a function to an event, not an instance.

Ah, I meant to say player.CharacterAdded:Connect()

Yes, player.CharacterAdded:Connect() would work, as CharacterAdded is an event

That wouldn’t work for your intended purpose because Characteradded is an event and thus does not return the Character.

player.CharacterAdded:Connect(function(char) doesn’t work?

It wouldn’t work, but you could get something like that to work if you made a function that returned the character, like this:

local Char = player.Character or player.CharacterAdded:Connect(function(char)
return char
end)

The reason why your line wouldn’t work is because you’re creating a function but you aren’t returning any value and so the variable would be set to nil.

Still wouldn’t quite work. What’s wrong with using

local char = player.Character or player.CharacterAdded:Wait()

Oh I see, you’re reffering to getting the character using the event. I was referring to using the event in general.

I got confused on the op’s post. I thought he meant:
local Character = player.Character
player.CharacterAdded:Connect()
and not:
local Character = player.Character or player.CharacterAdded:Connect()

I don’t think there’s a problem with using that but OP was trying to use :Connect in the line and I tried to build around it.

1 Like

Trying to see if it is possible to get the player’s character into a variable without using something like

local Char = player.Character
player.CharacterAdded:Connect(function(char)
Char = char
end)

Ran some tests with the script you provided however it actually does not seem to work

First test:

Test1A

Second Test:

Test2

Just gonna guess that my question is not a possibility at this point, unless I did something wrong in the test

local char = player.Character or player.CharacterAdded:Wait()

This should work for that

Wouldn’t that script only run once though? Considering if the player died that is.

If you want it to run again every time the player dies, you’ll have to put the whole thing inside of a CharacterAdded event.

2 Likes

Ah, I see. Thanks for the info

Yes your second test is wrong. Connect returns RBXScriptConnection. The return value is dropped because the engine code that fires CharacterAdded does not expect, need or use a return value. Char is being defined as the return of Connect, not of the function.

2 Likes