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

How do I use a variable without roblox assuming that it's the name of the item I'm trying to access?

local char = game.Players.LocalPlayer.Character
pcall(function()
	local DeletedWings = {
		"Angel", 				-- Empyrean Reignment
		"FireWings"			--Wings of Fire
	}
	for index,value in ipairs(DeletedWings) do
		local hatName = DeletedWings[index]
		print(hatName)
		while wait() do
			local X = DeletedWings[index]
			if char.X then --Errors here
			char.hatName:Destroy()
			end
		end
	end
end)

My goal is to loop through the array named DeletedWings until it detects a certain hat, then removes it. I believe it errors in the line "if char.X then", and I get the message "X is not a valid member of Model". I'm pretty sure that the script is searching for a "game.Players.LocalPlayer.Character.X" instead of a "game.Players.LocalPlayer.Character.Angel" How do I make the script use the variable's value (ie "Angel" or "FireWings" instead of its name?

3 comments

0

Change it to 'if X then'. It's searching for an object called 'X' in the character, not checking to see if X exists.

0

Also, I don't see that while loop terminating ever.

0

If you're trying to find an object with the name of X, you should use char:FindFirstChild(X).

1 answer

✓ Accepted answer

You might want to use if char:FindFirstChild(X) then for line 12.

And for line 13, you can use char[X]to refer to the child of the character.

3 comments

2

Why the concatenation? char[X] should work fine.

0

^

0

edited to remove unnecessary concatenation 'char[X .. ""]'

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