Is assigning a variable to player.Character faulty?

i will attempt to explain this the best i can
i will give you script 1, and script 2
script 1 does not work, script 2 does work, however the scripts are not the focus, only player.Character being a variable
only look at the one line below – interest here –

SCRIPT 1, doesn’t work

local function assignWeapon(player, weapon)
	local charpos = player.Character:WaitForChild("HumanoidRootPart").CFrame
        -- interest here--
        local char = player.Character

	-- if character
	if weapondata[weapon].character then
		local model = weaponfolder[weapon]:FindFirstChild(weapon .. "Model"):Clone()

		model.Name = player.Name
        -- interest here --
		char = model -- doesn't work, doesn't apply the custom character

		model.Parent = workspace
		player.Character:WaitForChild("HumanoidRootPart").CFrame = charpos
	end
end

----------- MAIN -----------
weaponevent.OnServerEvent:Connect(function(player, weapon)

	assignWeapon(player, weapon)
end)

SCRIPT 2, does work

local function assignWeapon(player, weapon)
	local charpos = player.Character:WaitForChild("HumanoidRootPart").CFrame
    -- LACK OF char = player.Character

	-- if character
	if weapondata[weapon].character then
		local model = weaponfolder[weapon]:FindFirstChild(weapon .. "Model"):Clone()

		model.Name = player.Name
        -- interest here
		player.Character = model -- does work, applies the custom character

		model.Parent = workspace
		player.Character:WaitForChild("HumanoidRootPart").CFrame = charpos
	end
end

----------- MAIN -----------
weaponevent.OnServerEvent:Connect(function(player, weapon)

	assignWeapon(player, weapon)
end)

assigning a variable to player.Character simply doesn’t work
why is that?

2 Likes

What is the error? player.Character can be nil at times.

1 Like

The reason for this is pretty simple. Player.Character is referencing the property Character of Player. Meaning if you do Player.Character = something, you’re saying "set the character property of player to something.

Meanwhile, if you say char = Player.Character, char is just a variable with the value of the players Character (a model or nil). Setting char = something else just changes the value of what the variable char equals, not the actual character property of Player.

Kinda like how if you say player = thing, it doesnt change what the actual player object in game is, it just changes what that variable in your code equals.

4 Likes

many thanks
something so simple can be so hidden

In the first script, the line local char = player.Character creates a new local variable char and assigns it the value of player.Character. Later in the script, when you assign a new value to char with the line char = model, you are only changing the value of the local variable char, not the value of player.Character. This means that the player’s character is not being changed to the custom character.

In contrast, in the second script, you directly assign a new value to player.Character with the line player.Character = model. This changes the player’s character to the custom character as intended.

In summary, assigning a value to a local variable that was previously assigned the value of player.Character does not change the value of player.Character. To change the player’s character, you need to directly assign a new value to player.Character. I hope this helps clarify why the first script doesn’t work and the second script does! :blush:

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