When the player presses the GUI button, if they have enough coins, they can purchase the item, and the cost of the item is then subtracted from their coin value.
How I went about this is… firing a remote event if conditions for purchasing an item are met, then it takes the “itemcost” data with the fire and is supposed to remove the value from the player’s Coins.Value
What is the issue?
The coins are not being removed from the player’s Coins.Value.
I tried using print(itemcost) to see if it’s nil, but it isn’t nil and has the correct value. Not sure what I’m doing wrong here ._.
Part of the ClientScript w/in GUI
slot.ImageButton.MouseButton1Click:Connect(function(player)
-- Get the first child of the ViewportFrame.Model and set the itemName variable to its Name property
local itemModel = slot.ViewportFrame.Model:GetChildren()[1]
local itemName = itemModel.Name
local player = game.Players.LocalPlayer
print(player) -- This will print the value of player in the console
print(player.leaderstats)
-- Check if the slot has an item equipped
local shopitem = objDisplay:GetChildren()[1]
local itemcost = shopitem:FindFirstChild("Cost").Value
if equipped == true and itemcost > player.leaderstats:FindFirstChild("Coins").Value then
player.PlayerGui.GameInventory.DeleteOneMsg.Visible = true
player.PlayerGui.GameInventory.DeleteOneMsg.Msg.Text = "You don't have enough coins."
--player.PlayerGui.GameInventory.Frame.Visible = true
wait(2)
player.PlayerGui.GameInventory.DeleteOneMsg.Visible = false
else
if equipped == true and itemcost <= player.leaderstats:FindFirstChild("Coins").Value then
local event = game.ReplicatedStorage.InvEvents.PickUp
local purchaseitem = game.ReplicatedStorage.ShopEvents.Purchase
local item = itemName
local firstChild = itemModel
local itemdescription = firstChild:FindFirstChild("Desc").Value
event:FireServer(item, itemdescription)
purchaseitem:FireServer(itemcost)
PurchaseScript within ServerScriptService
game.ReplicatedStorage.ShopEvents.Purchase.OnServerEvent:Connect(function(player, itemcost)
print (itemcost)
if player.leaderstats.Coins.Value == itemcost then
player.leaderstats.Coins.Value -= itemcost
else
player.PlayerGui.GameInventory.DeleteOneMsg.Visible = true
player.PlayerGui.GameInventory.DeleteOneMsg.Msg.Text = "You don't have enough coins."
--player.PlayerGui.GameInventory.Frame.Visible = true
wait(2)
player.PlayerGui.GameInventory.DeleteOneMsg.Visible = false
end
end)
I changed that line in the PurchaseScript as you explained; however, it is still not working. In the video I shared, perhaps you can see if I’m doing something wrong?
I’m not changing anything with the GUI’s… at least, I don’t think I am?
I’m wondering if it’s an issue with the fact I’m changing the Leaderstats.Coins.Value when I’m running the game as opposed to actually “earning” the coins through gameplay?
Oh yes! When I change the Coins.Value, the coins GUI is changing—thank you for pointing that out!
I can share my leaderstats ServerScript & the LocalScript within the coins GUI if that could help:
CoinsDataStore ServerScript
*Basically saves the amount of coins the player has
local statList = {
{Name = "Coins", Type = "IntValue"};
}
local Players = game:GetService("Players")
local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
local function SavePlayer(player)
local dataToSave = {};
for _, stat in pairs(player.leaderstats:GetChildren()) do
dataToSave[stat.Name] = stat.Value
end
local success, err = pcall(function()
Datastore:SetAsync(player.UserId, dataToSave)
end)
if not success then
warn("[!] Error saving data for " .. player.Name)
end
end
local function NewStat(statType, statName, statParent)
local new = Instance.new(statType)
new.Name = statName
new.Parent = statParent
return new
end
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
for _, statInfo in pairs(statList) do
local success, err = pcall(function()
NewStat(statInfo.Type, statInfo.Name, leaderstats)
end)
if not success then
warn("Error creating stat " .. tostring(statInfo.Name) .. ": " .. tostring(err))
end
end
local data; local success, err = pcall(function()
data = Datastore:GetAsync(player.UserId)
end)
if success and data then
for statName, statValue in pairs(data) do
if leaderstats:FindFirstChild(statName) then
leaderstats[statName].Value = statValue
end
end
elseif err then
warn("[!] Error loading data for " .. player.Name .. ": " .. tostring(err))
end
end)
Players.PlayerRemoving:Connect(SavePlayer)
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
SavePlayer(player)
end
end)
LocalScript in CoinGUI
local player = game.Players.LocalPlayer
local text = script.Parent
while true do
text.Text = "Coins: ".. player.leaderstats:WaitForChild("Coins").Value
wait(1)
end
i think because you change the coins value in the explorer and it only changes for the client because it was client side window and the server still sees 0 coins but if you switch to server side by click here
then you can change the coin value in explorer for server and it will probably fix the problem
The problem is that you are changing the money on the client side, the current one appears at the top (client or server) and you put it on the server and that’s when you change the money, then you go back to the client and test it