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

Why isn't the dependencies working?

I am scripting a tycoon and I am implementing dependencies to the buttons. I have the dependency values in the buttons set. It doesn't give out any errors, but it still doesn't work.

It makes of all of the buttons except for the first one disappear(which is what I want), but when I buy it, the other buttons don't appear. To make it disappear, I changed the transparency to 1 and the can collide to false.

There is a ServerScript in the tycoon folder that handles the buttons and the dependencies.

For the dependencies, I'm using coroutine.resume(coroutine.create(function(), but for some reason, it doesn't work. Can anyone explain why, or what I should use, please? Thanks.

Here is the code: (this is just part of the script, but the whole part that handles the dependencies.)

if v:FindFirstChild("Dependency") then
			v.Head.CanCollide = false
			v.Head.Transparency = 1
			coroutine.resume(coroutine.create(function()
				if script.Parent.PurchasedItems:WaitForChild(v.Dependency.Value) then
					v.Head.CanCollide = true
					v.Head.Transparency = 0
				end
			end))
		end

Any help is appreciated.

2 comments

0

on line 5, you should change :WaitForChild() to :FindFirstChild()

0

If you want a full explanation on how it works check out my answer, it teaches you how to implement the function in daily coding.

2 answers

It's because you're trying to use :WaitForChild() in an if statement. :FindFirstChild() is what you're looking for, since it returns a bool. :WaitForChild() returns an instance. Also, a coroutine isn't necessary here since the if statement doesn't contain any code that would yield the script.

if v:FindFirstChild("Dependency") then
	v.Head.CanCollide = false
	v.Head.Transparency = 1
	if script.Parent.PurchasedItems:FindFirstChild(v.Dependency.Value) then
		v.Head.CanCollide = true
		v.Head.Transparency = 0
	end
end

If nothing happens still, then that item doesn't exist and therefore that line gets skipped.

4 comments

0

k

0

I kind of dislike this answer since he's not going to learn what :FindFirstChild's use is.

0

um, yes i did. i literally wrote that FirstFirstChild returns a bool. my answer is still very correct, so you're falsely downvoting.

1

@maxpax2009 His answer is indeed correct as he is stating why he should use FindFirstChild instead of WaitForChild and then he prompts the fixed code. He also gave him a tip about why he doesn't have to use coroutine.

#:FindFirstChild

FindFirstChild works and functions like an if statement fx.

#How to utilize and implement FindFirstChild into daily coding;

if hit.Parent:FindFirstChild("Humanoid") then

Okay, so this checks if there is a humanoid or not.

If there is a humanoid it will work, else if there's not any humanoid it will not error. So that concludes if we did this:

if hit.Parent.Humanoid then

Then it would give us this error;

" 'Humanoid', is not a valid member of Model" So it's like a pcall, xpcall.

#FINISHED CODE So now to finish off your code;

if v:FindFirstChild("Dependency") then
	            v.Head.CanCollide = false
	            v.Head.Transparency = 1
	            coroutine.resume(coroutine.create(function()
if script.Parent.PurchasedItems:FindFirstChild(v.Dependency.Value) then
	  v.Head.CanCollide = true
	    v.Head.Transparency = 0
	         end
       end))
end

#CONCLUSION.

That concludes our answer, hope you have something new in your noggin when you read this.

Enjoy!

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