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
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.