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

Cash is not a valid member of Part "Part"?

Hey, i am having an issue with the new tycoon i am making. But there is a cash value in the part.

Code:

script.Parent.Touched:Connect(function(Part)
	local db = false
	if db == false then
		db = true
		if Part:FindFirstChild("Cash") then
			local Partdecor = Instance.new("ParticleEmitter",script.Parent)
			Partdecor.Texture = "http://www.roblox.com/asset/?id=300899516"
			Partdecor.Lifetime=NumberRange.new(1,2)
			Partdecor.Rate = 2
			if Part.Parent == nil then
				wait(1)
				Partdecor:Destroy()
			end
			spawn(function()
				script.Parent.Parent.Parent.CashToCollect.Value = script.Parent.Parent.Parent.CashToCollect.Value + Part.Cash.Value
				Part:Destroy()
			end)	
		end
		wait(.2)
		db = false
	end
end)
  • Error : Cash is not a valid member of Part "Part"

1 comment

0

dont forget to accept my answer if it worked!

2 answers

Fixed!

script.Parent.Touched:Connect(function(Part)
    local db = false
    if db == false then
        db = true
        if Part:FindFirstChild("Cash") then
            local Partdecor = Instance.new("ParticleEmitter",script.Parent)
            Partdecor.Texture = "http://www.roblox.com/asset/?id=300899516"
            Partdecor.Lifetime=NumberRange.new(1,2)
            Partdecor.Rate = 2
            if Part.Parent == nil then
                wait(1)
                Partdecor:Destroy()
            end
            spawn(function()
                script.Parent.Parent.Parent:FindFirstChild("CashToCollect").Value = script.Parent.Parent.Parent:FindFirstChild("CashToCollect").Value  + Part:FindFirstChild("Cash").Value
                Part:Destroy()
            end)    
        end
        wait(.2)
        db = false
    end
end)

Basically, I just changed this to :FindFirstChild!

EDIT: Changed script.Parent.Parent.Parent.CashToCollect.Value to script.Parent.Parent.Parent:FindFirstChild("CashToCollect").Value

4 comments

0

Now i have the error: Workspace.Newer Tycoon.Tycoons.Red Team.Essentials.Collector.Script:17: attempt to index nil with 'Value' 

0

okie

0

Fixed!

0

noice!

Try doing this code (FindFirstChild might not work):

script.Parent.Touched:Connect(function(Part) -- part is the part that touched the script's Parent
	local db = false
	if db == false then
		db = true
		if Part:FindFirstChild("Cash") then
			local Partdecor = Instance.new("ParticleEmitter") -- don't use second argument of "Instance.new"
			Partdecor.Texture = "http://www.roblox.com/asset/?id=300899516"
			Partdecor.Lifetime = NumberRange.new(1,2) or math.random(1,2) -- i don't know what "NumberRange" is
			Partdecor.Rate = 2
			Partdecor.Parent = script.Parent
			if Part.Parent == nil then
				wait(1)
				Partdecor:Destroy()
			end
			spawn(function()
				local cash = Part:FindFirstChild("Cash") or Part:FindFirstChild("Cash", true)
				if cash == nil then
					cash = Part:WaitForChild("Cash",10)
					if cash == nil then -- double check
						warn("Cash is nil, returning function...")
						return
					end
					
					script.Parent.Parent.Parent.CashToCollect.Value += cash.Value -- the same thing as the long add code
				end
				
				script.Parent.Parent.Parent.CashToCollect.Value += cash.Value
				Part:Destroy()
			end)
		end
		wait(.2)
		db = false
	end
end)

Hope this helps!

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