Remotefunction not working

im making a tower defense game and while making the upgrade system, the placeholder tower remains after placing until you attempt to place it on an invalid space

when the remotefunction is invoked and placingtowers = true, the code below wont run

these are snippets from the code
the part that isnt working is the “placingTowers” variable


			if canplace then --this part is in a localscript
			
			
				local placingTowers = SPAWNTHETOWER:InvokeServer(towertospawn.Name,towertospawn.PrimaryPart.CFrame) -- this is the part that wont work
		
			
				if  placingTowers then
						
						placedtowers += 1
					gui.towers.Parent.counter.Text = placedtowers .."/".. maxtowers
					RemoveplaceholderTower()
					toggleTowerinfo()
			
				end
		
			end
GETTHETOWER.OnServerInvoke = tower.Spawn --this part is in a modulescript
2 Likes

I think the OnServerInvoke part is incorrect. Try:

GETTHETOWER.OnServerInvoke:Connect(function(player, towerName, locationCFrame)
	print(player, towerName, locationCFrame)
end)

That should give you the data you need to then do the placement I think.

1 Like

Shouldn’t it be:

GETTHETOWER.OnServerInvoke = function(player, towerName, locationCFrame)
	print(player, towerName, locationCFrame)
end
1 Like

Yes, it should. A RemoteFunction is a function. You cant connect a function to a function. But you can try printing to see if your canplace variable is even true.

2 Likes

ive found that when placingTowers is true, the code below it will not run and i cant figure out why

Try to put a wait(4) after the SPAWNTHETOWER:InvokeServer

You can also try to print the value of placingTowers to understand what’s happening

the placeholder tower still remains after the tower is placed and with the wait(4) there is a 4 second delay when trying to place a tower

What it says if you print(placingTowers) ?

it only prints “false” when you run out of cash

Do you mean if you run this code it prints true ?

if  placingTowers then
					
    placedtowers += 1
   gui.towers.Parent.counter.Text = placedtowers .."/".. maxtowers
   RemoveplaceholderTower()
   toggleTowerinfo()
		
else

   print(placingTowers)

end

			if canplace then
			
				local placedTower = SPAWNTHETOWER:InvokeServer(towertospawn.Name,towertospawn.PrimaryPart.CFrame)
	print(placedTower)
			
				if placedTower then
						
						placedtowers += 1
					gui.towers.Parent.counter.Text = placedtowers .."/".. maxtowers
					
					toggleTowerinfo()
					
				end
			
			end

this is what is supposed to run
but it wont print anything unless placedTowers is false

Ok now i understand. It looks like in the line where you invoke the server it goes in a loop.
In your remote function try to not only exute the code but also at the and a return true

Like this:

local function spawnTower()
tower.Spawn --this part is in a modulescript
return true
end

GETTHETOWER.OnServerInvoke = spawnTower

still doesnt work and i cant figure it out

Are you getting any type of errors ?
Also i forgot to put the () in tower.Spawn did you fixed it ?

like this:
tower.Spawn()

image
nil and ‘scour’ are the towers you are trying to place
‘poor’ and ‘tower is not real’ happen when you dont have enough money
true is placedTower’s value and despite being true, the code will not run
placedTower wont print until you run out of money or some other placement error

1 Like

“true is placedTower’s value and despite being true, the code will not run”

Is it the one print placed after the invokeserver ?


yeah

You can try passing its value to another variable and try:

local value = placedTower

if value then
–code
end

:smiling_face_with_tear:

What is the code for tower.Spawn? Judging from the comments above, it seems like that function is yielding, which doesn’t allow the code after to run.