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

Why am I receiving this error in my GateControl script?

Hi I am making a tycoon and in my GateControl Script I have encountered an error basically the error is whenever I go to the gate and touch it, in the output it says Owner is not a valid member of Model so I would like an answer as to why my script is not working here is the script:

script.Parent.Head.Touched:connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent) 
	if player ~= nil then
		local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name)
		if cashmoney ~= nil then
			local ownstycoon = cashmoney:FindFirstChild("OwnsTycoon")
			if ownstycoon ~= nil then
				if ownstycoon.Value == false then
					if script.Parent.Parent.Parent.Owner.Value == nil then
						if hit.Parent:FindFirstChild("Humanoid") then
							if hit.Parent.Humanoid.Health > 0 then
								script.Parent.Parent.Parent.Owner = player
								ownstycoon.Value = true
								script.Parent.Name = player.Name.. "'s Tycoon"
								script.Parent.Head.Transparency = .6
								script.Parent.Head.CanCollide = true
								player.TeamColor = BrickColor.new(script.Parent.Parent.Parent.Name)
							end
						end
					end
				end
			end
		end	
	end		
end)

If someone could take their time to read through this and find an answer to this I would greatly appreciate this, Thanks.

2 comments

0

Mind posting an image of the file hierarchy? I'm guessing your error is being thrown on line 9.

0

Use "and"s like this. if game:FindFirstChild("skdksk") and game:FindFirstChild("ksksksmd") then

1 answer

The error is on line 9 - and I won't hesitate to say that is because you probably indexed too many (or not enough) parents, and your code is disorganized.

What you want to do is define variables before the functional part of your code, so you can easily organize everything and make your code more efficient. You get sick of indexing parents really fast.

local gate = script.Parent
local gateHead = gate:WaitForChild("Head")
local gateHolder = gatePart.Parent.Parent -- etc
local ownerVal = gate:WaitForChild("Owner") -- if the value has not loaded yet
local moneyStorage = game.ServerStorage:WaitForChild("MoneyStorage")

gateHead.Touched:connect(function(hit)
	local p = game.Players:GetPlayerFromCharacter(hit.Parent)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if p and human and human.Health > 0 then
		local dataHolder = moneyStorage:FindFirstChild(p.Name)
		if dataHolder then
			local ownsTycoon = dataHolder:FindFirstChild("OwnsTycoon")
			if ownsTycoon and ownsTycoon.Value == false and ownerVal.Value == nil then
				ownerVal.Value = p
				gate.Name = p.Name.. "'s Tycoon"
				head.Transparency = 0.6
				head.CanCollide = false
				p.TeamColor = BrickColor.new(gateHolder.Name)
			end
		end
	end
end)

You should be able to read your code easier now.


Hope I helped!

~TDP

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