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

Why does my hat Array variable not work with my script?

So, I've been making this script for about a month now. I'm constantly changing things and trying to make it better. Today, however, I tried to add something extra. Here's the code:

local part = script.Parent
local db = false
local myVar = game.Workspace.Pro.SurfaceGui.Test
local Alarm = game.Workspace.Alarm1
local myArray = {"Ultra-Fabulous Hair", "EpicWings", "NinjaTwinSwords", "DOGE", "Snake Eyes", "GlassesBlackFrame", "Americasweetheart"}


	


function FindHat(hit)
	local hat = hit.Parent:FindFirstChild(myArray[1]) or (myArray[2])
	local human = hit.Parent:FindFirstChild("Humanoid")
	    if not db then
	      if human and hat then
		
			db = true
			
			Alarm.Noise:Play()
		 
			
		myVar.Text = ("HOW DARE YOU WEAR THAT WRETCHED HAT!?")
	    wait(3)
		myVar.Text = ("YOU WILL BE SMITED BY MY WRATH!")
		wait(3)
		myVar.Text = ("DIE!!")
	    wait(2)
		human.MaxHealth = 0
		wait(5)
		myVar.Text = ("  ")
		
		game.Workspace.Alarm1.Noise:Stop()
		     
	     
		
		db = false
		
	
		
	else
		   
			myVar.Text = ("You're good to go!")
			wait(3)
			myVar.Text = ("    ")
			
			
			
		
		
		
		end
	end	
end


part.Touched:connect(FindHat)

Even though I'm not wearing these hats, the whole script runs on anyway. So, I reversed it and deleted:

local myArray = {"Ultra-Fabulous Hair", "EpicWings", "NinjaTwinSwords", "DOGE", "Snake Eyes", "GlassesBlackFrame", "Americasweetheart"}

and also changed

local hat = hit.Parent:FindFirstChild(myArray[1]) or (myArray[2])

to

local hat = hit.Parent:FindFirstChild("NameOfHat")

So, it started working again normally. Any ideas or suggestions as to why it didn't work with the previous parts?

2 comments

0

This script took you a month to create?

0

I've been working on for 1 month. I'm learning scripting by using what I learn, and practicing it into one big script. If I find I can't insert certain parts, I make a new script. I'm new to all this. I've only been practicing scripting for about 2 months. This script here took a month to make it end up where it is right now. I keep changing things in it.

1 answer

✓ Accepted answer

You're using or like you're talking to a person.

Lua doesn't understand what you mean -- or could mean many different things in many different contexts, and Lua doesn't try to figure out which one you want -- if you want anything other than the most basic interpretation (which is only valid on booleans) you must be explicit:

local hat = hit.Parent:FindFirstChild(myArray[1]) or hit.Parent:FindFirstChild(myArray[2])

If you want to check all of your hats, you can write a loop.

function findHat(character)
	for _, hat in pairs(myArray) do
		if character:FindFirstChild(hat) then
			return character:FindFirstChild(hat)
		end
	end
end

Names like myArray convey nothing about what the variable actually stores. You shouldn't need words like array or dictionary or table or list in your variable names.

hatNames is a much better name.

3 comments

0

So, can I keep adding on to what you are doing? Meaning: local hat = hit.Parent:FindFirstChild(myArray[1]) or hit.Parent:FindFirstChild(myArray[2]) or hit.Parent:FindFirstChild(myArray[3]) ect.?

0

You can, but it would be better to write a loop.

0

Ok, thanks

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