RemoteFunction is not working "InvokeServer()"

  1. What do I want to achieve?
  • I want the client to spawn the tower by clicking a button or pressing a number based on their order.
  1. What is the issue?
  • For some reason, everything was working perfectly earlier. Now this remote function stopped working randomly and it isn’t calling for a return, it seems that the function in the ModuleScript doesn’t want to receive the remote’s call.
  1. What solutions have I tried so far?
  • I’ve tried to change some variables to see if I miss spelled something, it seems that everything is correctly spelled because I haven’t changed anything inside the spawn tower function before this problem occurred.
  • I asked to other devs in a discord group which they made a tutorial about how to make a tower defense game, they didn’t understand the mistake and couldn’t help me.
  1. Anything important you should know?
  • Yes, basically all the rest of the game is working; mobs spawning, waves, time, loaded map, etc. All except the tower spawn.
  • There are no errors in the output, but the line provoking this issue is the:
local allowedToSpawn = requestTowerFunctions:InvokeServer(Defender.Name)

You will see this line here in the Local Script to see what I mean:

Local Script:

local requestTowerFunctions = functions:WaitForChild("RequestSpawn")
local getDataFunc = functions:WaitForChild("GetData")

local playerData = getDataFunc:InvokeServer()
	for i, tower in ipairs(playerData.SelectedDefenders) do
		local Defender = towers:FindFirstChild(tower)
		if Defender then
			
			local button = gui.MainBar.Content.Defenders.Template:Clone()
			local config = Defender:WaitForChild("Config")
			local profile = Defender:WaitForChild("Profile")
			local statistics = Defender:WaitForChild("Stats")

			button.Name = Defender.Name
			button.Icon.Image = profile.Icon.Texture
			button.DefenderTag.Text = i
			button.Price.Text = "$" .. config.Price.Value
			button.Price.Visible = true
			button.Tag.Value = i
			button.Visible = true
			button.Parent = gui.MainBar.Content.Defenders

			button.Interact.Activated:Connect(function()
				local allowedToSpawn = requestTowerFunctions:InvokeServer(Defender.Name)
				if allowedToSpawn then
					AddPlaceholderTower(Defender.Name)
				end
				button.SelectionEffect.Visible = true
				TweenService:Create(button.SelectionEffect, TweenInfo.new(0.2), {Size = UDim2.new(1.3, 0,1.3, 0)}):Play()
				TweenService:Create(button.SelectionEffect, TweenInfo.new(0.2), {BackgroundTransparency = 1}):Play()
				task.wait(0.2)
				button.SelectionEffect.Visible = false
				TweenService:Create(button.SelectionEffect, TweenInfo.new(0), {Size = UDim2.new(1, 0,1, 0)}):Play()
				TweenService:Create(button.SelectionEffect, TweenInfo.new(0), {BackgroundTransparency = 0}):Play()
			end)

			UserInputService.InputBegan:Connect(function(input, processed)
				if processed then
					return
				end

				if input.KeyCode.Value == button.Tag.Value + 48 then
					local allowedToSpawn = requestTowerFunctions:InvokeServer(Defender.Name)
					if allowedToSpawn then
						AddPlaceholderTower(Defender.Name)
					end
					button.SelectionEffect.Visible = true
					TweenService:Create(button.SelectionEffect, TweenInfo.new(0.2), {Size = UDim2.new(1.3, 0,1.3, 0)}):Play()
					TweenService:Create(button.SelectionEffect, TweenInfo.new(0.2), {BackgroundTransparency = 1}):Play()
					task.wait(0.2)
					button.SelectionEffect.Visible = false
					TweenService:Create(button.SelectionEffect, TweenInfo.new(0), {Size = UDim2.new(1, 0,1, 0)}):Play()
					TweenService:Create(button.SelectionEffect, TweenInfo.new(0), {BackgroundTransparency = 0}):Play()
				end
			end)
		end
	end

ModuleScript Function:

function tower.CheckSpawn(player, name, previous)
	print(name)
	local towerExists = ReplicatedStorage.Towers:FindFirstChild(name, true)

	if towerExists then
		if towerExists.Config.Price.Value <= player.Coins.Value then
			if previous or player:WaitForChild("TowersPlacesLimit"):FindFirstChild(towerExists.Profile.DefenderName.Value).Placements.Value < player:WaitForChild("TowersPlacesLimit"):FindFirstChild(towerExists.Profile.DefenderName.Value).Limit.Value then
				if previous or player.PlacedDefenders.Value < workspace.Info.MaxDefenders.Value then
					return true
				else
					maxDefendersReached:FireClient(player)
					warn("Max Limit of Defenders reached!")
				end
			else
				maxLimitReached:FireClient(player)
				warn("Defender has reached max limit")
			end
		else
			cantAfford:FireClient(player)
			warn("Player cannot afford")
		end
	else
		warn("Defender does not exist")
	end

	return false
end

requestTowerFunctions.OnServerInvoke = tower.CheckSpawn

This is the tutorial I watched to make this game:
How to make a Tower Defense Game - #1 Path Navigation - YouTube

I made a lot of effort to make this game, even hired someone to help me on art. I will not waste my project for one error that breaks the important function of this tower defense game. I assume this is a silly mistake since its only one line, but I haven’t found a solution, it’s been 2 days, can anybody help me please? Thank you very much.

7 Likes

Could you show the location of the module scripts if you’ve changed it before it stopped working? If not I’ve got a couple of things I think you should try:

Is it making it past this line:

Try adding a print statement and passing the returned value (allowedToSpawn variable).

Try adding two parenthesis to the tower.CheckSpawn method above (tower.CheckSpawn()). This will probably result in an error but why not give it a shot.

And if all the above doesn’t work:

Try to find a pcall which could be resulting in the error being covered up (if you find a pcall and it starts to error, either reply or attempt to fix the error yourself).

It could also be the cause of something else blocking the InvokeFunction event so maybe look at your other code.

3 Likes

Yes, I tried adding a print before and after that line, but nothing works after the invokeserver line.

The second option may not work but I will try.

I never used pcall before, but if that solves the problem then I gotta start using it, i will try all options.

2 Likes

With pcalls, you usually use them for functions like SetAsync, GetAsync, UpdateAsync, etc. They don’t show the error in the output, but that specific section of code won’t work. It’s really just used so the rest of your code won’t be affected by that specific section imo.

GetAsync and those other functions error occasionally so it’s best to wrap them in pcalls so the rest of your code can run.

I’d recommend only using it when you know that some code will error but will work eventually. I’m not the best at explaining stuff so sorry if this is a bit confusing :sweat_smile:

Also, maybe check out this line:

This might be stopping the code after that (the in pairs loop utilizing the player data) from running and not allowing your other RemoteFunction from running.

Try printing the returned value from that RemoteFunction.

This isn’t really a whole #resources:community-tutorials on pcalls so I won’t go more into depth.

Also, here’s a useful post about pcalls.

3 Likes

When I print the playerData, it prints all the data I have. That part works.
Actually, it IS putting my equipped towers to the backpack. The mystery is in the allowedToSpawn line. And thanks for the info of pcalls I will read it and see if can be useful.

4 Likes

Could you show / describe what this button is? It doesn’t seem like a 2D TextButton (BillboardGui or ScreenGui).

This is the function that contains the RemoteFunction that isn’t running.

This could also be a reason for the code not running.

3 Likes

A reminder, the button detects the click and I can even switch places between effects and allowedToSpawn so the effects play first, but allowedToSpawn will keep rejecting me.

4 Likes

The button is created by a template frame, its in a ScreenGui, I made it in Frame instead of CanvasGroup now due to some bugs I experienced with it, but its still the same.
However, this are the buttons shown in the game:
image
And in their frames they have a TextButton whose name is Interact.
image

4 Likes

Maybe try changing this:

Into this:

button.Interact.MouseButton1Click:Connect(function()

Dunno if you purposely arent using this function but yeah I gtg soon so this is one of the last things I can provide… sorry :sweat_smile:

P.S. the artwork and fonts look really nice :eyes:

3 Likes

Lol no I’m serious, I haven’t changed anything in this script. I was just fixing a tower’s attack animation (which it was in a local script inside StarterPlayerScripts) that kept the proyectile in workspace when the tower was removed/upgraded. And when I played to test the result, boom, randomly it stopped spawning towers. It’s really weird cause it worked before the last attempt to fix.

I believe this is a roblox bug in general, but alright have a good night!

Thanks, that’s why I don’t want to abandon this game just because of this single invisible error that won’t be fixed.

4 Likes

Nope, MouseButton1Click didn’t fix it, I tried with MouseButton1Down too but either. The button is not the problem, is the Remote :disappointed_relieved:

2 Likes

Sorry for the super late reply;

Are you requiring the module script by a Script? That might be the reason it isn’t running.

You can get back to me whenever its a good time.

2 Likes

nono, the module script is required by another module script.

(excuse my late response too lol, i still havent found a solution)

2 Likes

This may sound silly but to cover all bases, is this line of code being used elsewhere, in other modules/scripts etc?
I had a similar issue a while back and having exhausted all methods of trying to fix it, I then realised I was using .OnServerInvoke for the same RemoteFunction in two different modules - it will only work in one.

2 Likes

No actually it only exists in that module script. In the local script there is another way to InvokeServer, which it is the keybind.
But you gave me the idea of moving the tower.CheckSpawn() to another place in the module script, in this case I put it above all the other functions, and still nothing.

2 Likes

When this bug happened for first time I tried switching the current version for an old one, right before making this new tower and it was fixed so i brought all the important changes to that version and keep working, and when I was fixing its attack the bug started again. This studio is for real cursed.

2 Likes

This problem is just impossible, others who followed this tutorial they never experienced this bug when I asked them if they did.

  • The button detects the click but not the remotefunction.
  • Even if I changed the remote’s name, the rest hasn’t changed.
  • Even if I moved the OnServerInvoke function to another line, nothing.
  • I used pcall but its not even printing the error, that line is cursed.

I’m totally guessing this is a roblox bug. I would accept any punishment if it was just a mistake :sob:

WOW! Not even by moving the function to another module script. This is unbelievable, a Roblox Administrator has to intervene in this urgently!

1 Like

Did you fix it? I’m having the same issue.

No, I haven’t tried anymore. I’m taking a break from it for now.

1 Like