Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Not sure how to handle this one...? [read]

local player = game.Players.LocalPlayer

player.Changed:connect(function(propertyChanged)
	if propertyChanged == player.Character then
		print("The character changed! omigod")
		if player.Character then --makes sure it isn't adjusting to a nil object!!!
			local object = player.Character['COM'] --looks for COM, where the camera focuses.
			if object then
				game.Workspace.CurrentCamera.CameraSubject = object
			else
				print("ERROR!!!!!1")
			end
		end
	end
end)

So basically what this script does is the following:

-Executes when a property of player changes -Checks if the propertyChanged argument is player.Character -Checks if the object exists in Workspace in the first place* -Sets local variable which uses the FFC method to look for a child named "COM" in the players char. -If it exists, then set the currentcamera focus to the object -Otherwise, produce an overwhelming error message

I tested it out, and it seems to have executed without any notation on what happened. The first print in the function .. ("The character changed! omigod") .. did not execute. Hence I assume that one of the errors is the if statement right after the function starts that checks if the propertyChanged argument is the player's character.

3 answers

Use player.CharacterAdded instead:

local Player=game.Players.LocalPlayer
Player.CharacterAdded:connect(function(character)
	local Object=character:FindFirstChild('COM')
	print(Object and "The thing exists!" or "The thing doesn't exist!")
end)

also, you make sure the localscript isn't being removed when the character respawns, e.g. by ResetPlayerGuiOnSpawn.

The key concept here is data types. You have strings, numbers, booleans, tables, functions, threads, userdata, etc. One data type can not equal another.

print(1 == "1") --False, 1 is a number, not a string
print(true == "true") --False, true is a boolean, not a string
print(workspace == "workspace") --False, workspace is a userdata, not a string

And in your case, you're comparing propertyChanged, a string, to player.Character, a userdata. Let's try this:

if player[propertyChanged] == player.Character then

or alternatively

if propertyChanged == "Character" then

What Changed passes is a string value. In method 1, you index a member of the player with a key equal to that string value. Essentially, you're saying player["Character"]. In the second method, you're just checking if the name of the property that changed is equal to the string, "Character".

I think (not 100% sure) your script is erroring because you're checking if the character model (your workspace character) properties have changed (archivable, primarypart, etc.)

This discussion is preserved as it was. New answers, comments, and votes are closed.