How do you Fire an event if it has an if statement with else in it?

Ex.

-- Local Script

local event = game.ReplicatedStorage:WaitForChild(“RemoteEvent”)

local string = “Clicked”

local db = true

script.Parent.MouseButton1Click:Connect(function()
     if db then db = false
        event:FireServer(string, db)
     else
         -- ???
        db = true
     end
end)

-- Server Script

local event = game.ReplicatedStorage:WaitForChild(“RemoteEvent”)

event.OnServerEvent:Connect(function(plr, string, db)
     if db then
         print(string)
     else
         print(string)
     end
end)

I am confused on what your asking? You would just fire an event normally, why would it be different due to it being inside of an else?

Because I want to anchor multiple parts inside a folder in the workspace by clicking a button. I used :GetChildren() but it just picks a random part inside the folder and not all of it. Tried using a loop but it’s still not working. No errors too

How would firing an event in the else part of the if statement affect that though?

In your case you would just do a loop via collecting all the children. If you want you can send the loop code and I can see if I can fix it for you.

So you want a client, to be able to press a button
and that button tell the server to anchor a bunch of parts in a folder?

1 Like

Just saying it has else because I want it when pressing the button, all parts anchors, when u press it again, it unanchors

well first of all, you don’t need to send db (if you are using it for debounce) to the server
just use it on the client to make sure the button can not be spammed

So how can I apply it through script?

-- Client Script

local db = true
local event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local buttonDelay = 5

script.Parent.MouseButton1Click:Connect(function()
	if db then 
		db = false
		event:FireServer("buttonClicked")
		wait(buttonDelay)
		db = true
	end
end)

-- Server Script

local event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

event.OnServerEvent:Connect(function(plr, message)
	if message == "buttonClicked" then
		print(message)
		--do your anchoring and unanchoring here
		--for _, part in pairs(workspace.PartFolder:GetChildren()) do
		-- part.Anchored = not part.Anchored
		--end
	end
end)
1 Like