Remote functions working in studio but not in game

I want to be able to figure out why my shop UI is not working in game, it works perfectly in studio, but the remote functions aren’t responding.

Sorry about this being rushed, if anything seems unclear i would be happy to provide any more information you need!

The remote functions i am currently using for my game currently work in studio but does not want to work in game.
Local Script
image
Functions
image
Server Script


I have a callback to both remote functions, like i said they work in studio but not in game for some strange reason.

Here are some gifs of me testing it out both in studio and in game:

Studio
https://gyazo.com/01bee0ffded35a0af425e9c9f4ea793d

In the actual game
https://gyazo.com/3e67a5f042245292be71f50eee1301ab

it does print before calling the function, but it doesn’t get to doing it for some reason!!!
image

Please send a formatted version of those scripts.


LOCALSCRIPT


	if buyButton.Text == "BUY" and not player.OwnedEquipment:FindFirstChild(selectedLoadout.Value) then
		print("buy")
		if BuyItem:InvokeServer(selectedLoadout.Value) == true then
			buyButton.Text = "EQUIP"
			game:GetService("TweenService"):Create(buyButton, TweenInfo.new(0.2), {BackgroundColor3 = Color3.fromRGB(255, 170, 0)}):Play()
			script.Buy:Play()
		else
			script.FailBuy:Play()
		end
	elseif buyButton.Text == "EQUIP" and player.OwnedEquipment:FindFirstChild(selectedLoadout.Value) then
		print("equip")
		if EquipEvent:InvokeServer(selectedLoadout.Value) == true then
			script.Equip:Play()
		end
	end
end)```



SERVERSCRIPT 
local function buyItem(player, equipment)
	if player and equipment then

		local foundItem
		local playersmoney = player:WaitForChild("Money")
		local playerRank = player:WaitForChild("Rank")
		
		for i, v in pairs(equipmentModule.equipmentTable) do
			if v[equipment] then
				foundItem = v[equipment]
			end
		end
		if foundItem and not player.OwnedEquipment:FindFirstChild(equipment) then
			local moneyNeeded = foundItem["Cost"]
			local rankNeeded = foundItem["Rank"]
			
			if playerRank.Value >= rankNeeded and playersmoney.Value > moneyNeeded then
				playersmoney.Value = playersmoney.Value - moneyNeeded
				local newValue = Instance.new("StringValue", player:WaitForChild("OwnedEquipment"))
				newValue.Name = equipment
				equipmentModule.SaveData(player)
				return true
			end
		else
			return false
		end
		
	end
end
local function equipItem(player, equipment)
	if player and equipment then
		local foundItem
		for i, v in pairs(equipmentModule.equipmentTable) do
			if v[equipment] then
				foundItem = v[equipment]
				break
			end
		end
		if foundItem then
			if foundItem["Class"] == "Primary" then
				player:WaitForChild("WeaponLoadout").PrimaryWeapon.Value = equipment
			elseif foundItem["Class"] == "Secondary" then
				player:WaitForChild("WeaponLoadout").SecondaryWeapon.Value = equipment
			elseif foundItem["Class"]== "Helmet" then
				player.Helmet.Value = equipment
			elseif foundItem["Class"] == "Equipment" then
				player.Equipment.Value = equipment
			end
			return true
		else
			return false
		end

	end
end


if not game:GetService("RunService"):IsStudio() then
	game:BindToClose():Connect(function()
		for i, Plr in pairs(game.Players:GetPlayers()) do
			equipmentModule.SaveData(Plr)
		end
	end)
end
game.ReplicatedStorage.EquipmentEvents.EquipItem.OnServerInvoke = equipItem
game.ReplicatedStorage.EquipmentEvents.BuyItem.OnServerInvoke = buyItem
1 Like

Server Script ^


I’m not exactly sure what you meant by “Remote functions not working”, but I found a problem when you were trying to save the player data. This is not how you properly BindToClose the game. BindToClose is not a normal event, so you cannot :Connect() it. Instead, you should try putting the function inside of the BindToClose brackets/first parameter (without connecting it).

game:BindToClose(function()
    -- Stuff here
end)

Full Server Script:

Script
local function buyItem(player, equipment)
	if player and equipment then

		local foundItem
		local playersmoney = player:WaitForChild("Money")
		local playerRank = player:WaitForChild("Rank")

		for i, v in pairs(equipmentModule.equipmentTable) do
			if v[equipment] then
				foundItem = v[equipment]
			end
		end
		if foundItem and not player.OwnedEquipment:FindFirstChild(equipment) then
			local moneyNeeded = foundItem["Cost"]
			local rankNeeded = foundItem["Rank"]

			if playerRank.Value >= rankNeeded and playersmoney.Value > moneyNeeded then
				playersmoney.Value = playersmoney.Value - moneyNeeded
				local newValue = Instance.new("StringValue", player:WaitForChild("OwnedEquipment"))
				newValue.Name = equipment
				equipmentModule.SaveData(player)
				return true
			end
		else
			return false
		end

	end
end
local function equipItem(player, equipment)
	if player and equipment then
		local foundItem
		for i, v in pairs(equipmentModule.equipmentTable) do
			if v[equipment] then
				foundItem = v[equipment]
				break
			end
		end
		if foundItem then
			if foundItem["Class"] == "Primary" then
				player:WaitForChild("WeaponLoadout").PrimaryWeapon.Value = equipment
			elseif foundItem["Class"] == "Secondary" then
				player:WaitForChild("WeaponLoadout").SecondaryWeapon.Value = equipment
			elseif foundItem["Class"]== "Helmet" then
				player.Helmet.Value = equipment
			elseif foundItem["Class"] == "Equipment" then
				player.Equipment.Value = equipment
			end
			return true
		else
			return false
		end

	end
end


if not game:GetService("RunService"):IsStudio() then
	game:BindToClose(function()
		for i, Plr in pairs(game.Players:GetPlayers()) do
			equipmentModule.SaveData(Plr)
		end
        task.wait(3)
	end)
end
game.ReplicatedStorage.EquipmentEvents.EquipItem.OnServerInvoke = equipItem
game.ReplicatedStorage.EquipmentEvents.BuyItem.OnServerInvoke = buyItem

Thanks a lot for the response!
I didn’t realise that
I figured out the problem eventually, the :BindToClose() function was stopping the rest of the script below from working, I instead put the two onserverinvokes above the bindtoclose function.