So I'm fairly new to coding and I'm trying to figure out how I can make it so it checks to see if there is a specific child. If there is said child for it to not do anything. This is probably the best way I'm going to be able to explain it currently.
I've tried this
local Activat = script.Parent local TemplateL = Activat.TemplateL local TemplateR = Activat.TemplateR local ActivCode = game.ServerStorage.Fireballclone local ActivCodee = game.ServerStorage.Power local function Open (part) local parent = part.parent if game.Players:GetPlayerFromCharacter(parent)then local activater = ActivCode:Clone() local activates = ActivCodee:Clone() activater.Parent = game.Workspace.Cannon activates.Parent = game.Workspace.Cannon end end local removed = workspace.Cannon:FindFirstChild("Power",true) if removed == true then wait(2) else TemplateL.Opener.Touched:Connect(Open) end
I know this is wrong but again I want to figure out a way for the script to be able to tell if there is already a "Power" child within workspace.cannon and if not for it to only work when there isn't a "Power" child already in workspace.cannon. Thank you.
Just in case. This script is meant to work in conjunction with this other script
local Activ = script.Parent local TemplateL = Activ.TemplateL local TemplateR = Activ.TemplateR local ActivCode = game.ServerStorage.Fireballclone local ActivCodee = game.ServerStorage.Power local function Close (part) local parent = part.parent if game.Players:GetPlayerFromCharacter(parent)then workspace.Cannon.Fireballclone:Destroy() workspace.Cannon.Power:Destroy() wait(1) end end TemplateR.Closer.Touched:Connect(Close)
From line 20 of your first script:
local removed = workspace.Cannon:FindFirstChild("Power",true) if removed == true then wait(2) else TemplateL.Opener.Touched:Connect(Open) end
At line 22, I can see that you're trying to compare a function that returns an Instance (if found) to a bool - which means the local variable "removed" will never be true
and therefore connects the function to a Touched event to the Opener without care.
--From line 22 if removed then --does the children exist? Yes? Yields for 2 seconds. wait(2) else --if the child doesn't exist or nil? --do your thing end --Instead of comparing local "remove" to a bool value (which is like comparing a variable to an exact value), you can instead do this method to check if the item exist or not a nil/false value.
You can also use a logical operator that determines if the child does not exist (also returns the opposite value if relevant):
--From line 22 if not removed then --does the children NOT exist? --do your thing end --Only use if you don't want to wait for 2 seconds if the child exists.
Now, those should've been a solution to your question. But not yet.
Well... I'm not gonna lie, but the answer above is just a minor fix of how you tried to compare a function that returns an Instance to a bool value, which is of no difference to comparing chalk and cheese. Now here's the real way to solve your problem:
Check if Power
is a child of workspace.Cannon
right when the local Open()
function is called.
Short demonstration:
local function Open() local power = workspace.Cannon:FindFirstChild("Power", true) if not power then --"Power" is not a child of workspace.Cannon --do your thing end end --Or, you can use "return" to stop the following execution of the function during the call. local function Open() local power = workspace.Cannon:FindFirstChild("Power", true) if power then --"Power" is a child of workspace.Cannon return --ignores the rest of the function end --do your thing end
Since your script only run once (except the connected function which will be fired by other sources), your attempt of finding the child will only perform once. What if Power
is not the child of workspace.Cannon
during the execution? It will wait for 2 seconds, then run the following lines of code without looking back. Pretty inconvenient, isn't it? Toggling the script on and off would be quite annoying.
To make it work, connect the function to the Touched
event immediately after the function is made instead of giving certain conditions that are only checked once.
local Activat = script.Parent local TemplateL = Activat.TemplateL local TemplateR = Activat.TemplateR local ActivCode = game.ServerStorage.Fireballclone local ActivCodee = game.ServerStorage.Power local function Open (part) local removed = workspace.Cannon:FindFirstChild("Power",true) --moved here --check if the "Power" is avalable, it will ignore unless it is removed. if removed then return --ignores the following lines of code in the function, returns nothing end local parent = part.parent if game.Players:GetPlayerFromCharacter(parent)then local activater = ActivCode:Clone() local activates = ActivCodee:Clone() activater.Parent = game.Workspace.Cannon activates.Parent = game.Workspace.Cannon --cannons are created. end end --This thread only executes once so... after this its job is done. In case if this one loops then it is necessary to remove all connections your created before starting a new loop sequence using :Disconnect(). --For now this is not necessary, moving on --Connects the Touched event, without any conditions, because it is already accounted by the function above. TemplateL.Opener.Touched:Connect(Open) --creates a connection --The rest of your script goes here --This is your script, so you have the rights to edit it if there are parts that you don't prefer doing.
You can do the same to the Close()
function you made.
Sometimes my words can be really confusing (with the fact that I'm not a native English speaker). Nonetheless, I hope that'll help you, and if that, umm, doesn't, then sorry in advance!