Calling remoteevents from the command bar?

I don’t know if you really need to read the whole thing, but here

--power up spawning remote event
game.ReplicatedStorage.spawnPowerUp.OnServerEvent:Connect(function(plr, powerUpName, position)
	print("fired")

	local powerUpTemplate = game.ServerStorage.powerUpTemplate:Clone()

	--loop through all powerups, seeing if it matches "powerUpName"
	local powerUps = game.ReplicatedStorage.powerUps:GetDescendants() --get all powerups

	local chosenPowerUp
	for i, powerUp in pairs(powerUps) do
		if powerUp.Name == powerUpName then
			chosenPowerUp = powerUp
			break
		end
	end


	--if no power ups match the given name, then give a message in the output
	if chosenPowerUp == nil then
		print("Power up " ..powerUpName .." not found!")

	else
		--otherwise, if a powerup was found, start customizing the powerUpTemplate
		powerUpTemplate.Name = chosenPowerUp.Name
		powerUpTemplate.tokenTemplate.rightSide.image1.Texture = "rbxassetid://" ..chosenPowerUp.Value
		powerUpTemplate.tokenTemplate.leftSide.image2.Texture = "rbxassetid://" ..chosenPowerUp.Value
		
		local rarity = chosenPowerUp.Parent.Name
		powerUpTemplate.tokenTemplate.BrickColor = rarityColors[rarity]

		--if a position was given, position it there
		if position ~= nil then
			powerUpTemplate:PivotTo(CFrame.new(powerUpTemplate.Hitbox.Position + position))
		end
		--NOTE: if a position was not specified, meaning it DOES equal nil, it will simply be positioned at the origin

		powerUpTemplate.Parent = workspace.powerUpSpawnFolder
	end



end)

I’m throwing the following into the command bar
game.ReplicatedStorage.spawnPowerUp:FireServer(“Persona”)
No errors, no nothing
That first print statement at the top? It isn’t running, meaning the remote event isn’t being trigger at all
Why?

1 Like

I had it under a while loop, meaning the script never even knew my code was there…

1 Like

A silly mistake

But, in the future. If you ever have stuff in a script inside of a while loop. Put it in a coroutine. It will allow the script to run the loop and run any code under it.

For example, this is a for loop that runs code for every button that exists in my game inside the main client script:

coroutine.wrap(function()
	for _, btn in pairs(localPlayer:WaitForChild("PlayerGui"):GetDescendants()) do
		if btn:IsA("TextButton") or btn:IsA("ImageButton") then
			local buttonSizeX = btn.Size.X.Scale
			local buttonSizeY = btn.Size.Y.Scale

			btn.MouseEnter:Connect(function()
				local newSizeX = (buttonSizeX + 0.05)
				local newSizeY = (buttonSizeY + 0.05)
				
				if sounds:FindFirstChild("ButtonSound") then
					sounds:WaitForChild("ButtonSound"):Play()
				end
				
				if btn:FindFirstChild("Details") then
					btn.Details.Visible = true
				end
				
				local info = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
				
				if btn.Name == "Close" then
					tweenService:Create(btn, info, {Size = UDim2.new(0.13, 0, 0.13, 0)}):Play()
				elseif btn.Parent.Name == "Tools" or btn.Parent.Name == "Pages" then
					tweenService:Create(btn, info, {Size = UDim2.new(buttonSizeX, 0, buttonSizeY, 0)}):Play()
				else
					tweenService:Create(btn, info, {Size = UDim2.new(newSizeX, 0, newSizeY, 0)}):Play()
				end

			end)

			btn.MouseLeave:Connect(function()
				if btn:FindFirstChild("Details") then
					btn.Details.Visible = false
				end
				
				local info = TweenInfo.new(0.4, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
				tweenService:Create(btn, info, {Size = UDim2.new(buttonSizeX, 0, buttonSizeY, 0)}):Play()
			end)
		end

	end


end)()

There are about 500 lines of code below this that can all run because of the coroutine.


Another example. In my leaderstats script, I have a function inside of the LoadData() function, which loads the leaderstats and any saved data. But, the function is to add 1500 cash to the player’s cash stat every 5 minutes if they have the VIP game pass. It is in a module script, and would stop any line of code underneath of it (this includes the code below finishing the function, and the SaveData() function itself.)

Here is the code for that:

coroutine.wrap(function()
	while player do
		task.wait(300)
		if marketPlaceService:UserOwnsGamePassAsync(player.UserId, vipGamePass) then
			player.leaderstats.Cash.Value += 1500
		end
	end
	
end)()

In the future, if you want to run code below a while loop or for loop, wrapping this code into a coroutine will make that loop run at all times, and will allow any code under it to run. It’s letting the script multitask essentially.


Take any loop you want, and just put it into this:

coroutine.wrap(function()
	
end)()

And after you do that, the script will multitask!


Very useful indeed.

Anyways, hope you appreciate this information (even if you already knew)
I gotta get back to work lol

Best regards,
RoboBoy013