hiiii, I have in game shop system which saves with the data storage. Recently it stopped working and it seems like even tho the code executes the item does not get in to player backpack
if itemType == "WEAPON" then
if credits.Value >= prize then
credits.Value -= prize
ServerStorage[item]:Clone().Parent = player.Backpack
NotifyEvent:FireClient(player, "Interaction Sucessfull", "You have bought "..item.." from black market.")
return true
else
NotifyEvent:FireClient(player, "Interaction Failed", "You can not afford "..item)
return false
end
To be specific script is returning true as well as fireing the NotifyEvent. However the player does not recieve the item. Lets define example item as P90
I have confirmed that data input is correct so in this example ServerStorage[P90]:Clone().Parent = player.Backpack
Should get P90 to player inventory but it does not
Is this script a LocalScript? If it is you will need a ServerScript to access ServerStorage. You could also move the tools into ReplicatedStorage to solve the problem too, because the client can access that.
Mayby I am not master of optimisation but i did stick it together with a little help of internet.
local ServerStorage = game:GetService("ServerStorage")
local event = game.ReplicatedStorage.Events:WaitForChild("ShopEvent")
local DatakeyWeapons = "Testing03092023" --Weapons; Revival Update
local WeaponData = game:GetService('DataStoreService'):GetDataStore(DatakeyWeapons)
local NotifyEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("Notify")
function LoadBoughtItems(player)
local success, result = pcall(function()
return WeaponData:GetAsync(player.UserId)
end)
if success and result ~= nil then
return game:GetService("HttpService"):JSONDecode(result)
else
return {}
end
end
function BuyItem(player, item)
local boughtItems = LoadBoughtItems(player)
print("Weapon has been bought")
table.insert(boughtItems, item)
local boughtItemsString = game:GetService("HttpService"):JSONEncode(boughtItems)
WeaponData:SetAsync(player.UserId, boughtItemsString)
end
function HasItem(player, item)
local boughtItems = LoadBoughtItems(player)
print(boughtItems)
for i, v in ipairs(boughtItems) do
if v == item then
return true
end
end
return false
end
function Shop(player, item, prize, IsShopPernament, itemType)
prize = string.gsub(prize, "%D", "")
prize = tonumber(prize)
print("Shop has recieved:" .. tostring(player) .. tostring(item) .. tostring(prize) .. tostring(IsShopPernament) .. tostring(itemType))
local credits = player:WaitForChild("Database"):WaitForChild("Credits")
if item == "RIOT" then
if credits.Value >= 300 then
credits.Value = credits.Value - 300
game.ReplicatedStorage.Events:WaitForChild("ForceRiot"):Fire()
end
elseif item == "[UNAVAILABLE]" then
print("Item Unavailable")
return
else --if none applys
if IsShopPernament == false then
if itemType == "WEAPON" then
if credits.Value >= prize then
credits.Value -= prize
ServerStorage[item]:Clone().Parent = player.Backpack
NotifyEvent:FireClient(player, "Interaction Sucessfull", "You have bought "..item.." from black market.")
return true
else
NotifyEvent:FireClient(player, "Interaction Failed", "You can not afford "..item)
return false
end
elseif itemType == "ARMOR" then
if credits.Value >= prize then
credits.Value -= prize
shared.GiveArmor(player, ServerStorage:WaitForChild("WearArmor"):FindFirstChild(item))
NotifyEvent:FireClient(player, "Interaction Sucessfull", "You have bought "..item)
else
NotifyEvent:FireClient(player, "Interaction Failed", "You can not afford "..item)
end
return true
else
warn("Attempted to buy a tool which does not exist")
NotifyEvent:FireClient(player, "Interaction Failed", "An Error has occured. Please report it to manufacturing department member. ")
return false
end
else -- Shop is pernamnet
if ServerStorage:FindFirstChild(item) ~= nil then
if HasItem(player, item) then
if player.Backpack:FindFirstChild(item) == nil then
print("a")
local u = ServerStorage[item]
u:Clone().Parent = player.Backpack
else
print("b")
end
NotifyEvent:FireClient(player, "Interaction Sucessfull", "You have equipped "..item)
return true
elseif prize ~= nil and credits.Value >= prize then
credits.Value -= prize
BuyItem(player,item, prize)
NotifyEvent:FireClient(player, "Interaction Sucessfull", "You have bought "..item)
return true
elseif prize == nil then
NotifyEvent:FireClient(player, "Interaction Error", "Something went wrong when getting price of requested item, price == nil")
return false
else
print()
NotifyEvent:FireClient(player, "Interaction Failed", "You can not afford "..item)
return false
end
end
end
end
end
game.ReplicatedStorage.Events.ShopEvent.OnServerEvent:Connect(Shop)
And let me know if it works or what happens. This will help determine the problem that’s happening, because right now I’m not sure why that one line isn’t working
I did that and player still does not get the tool. I am quite confused why is this happening as it was working before flawlessly, I will try to look thru recently added scripts but from what i remember I didn’t touch anything regarding inventory.
If it’s only changing on the client then there is probably some local script somewhere effecting it. I suggest you check any local scripts that have a chance of destroying tools. If you don’t have a local script anywhere else that is messing it up then I’m completely lost
Alright! Issue has been resolved. Apparently I am an absolute idiot and the shop UI script had following lines:
function RemovePlayerToolsPrim()
for itemName, prize in pairs(configPrim) do
if plr.Backpack:FindFirstChild(itemName) ~= nil then
plr.Backpack:FindFirstChild(itemName):Destroy()
end
end
end
function RemovePlayerToolsSec()
for itemName, prize in pairs(configSec) do
if plr.Backpack:FindFirstChild(itemName) ~= nil then
plr.Backpack:FindFirstChild(itemName):Destroy()
end
end
end
These would be initialized on CLIENT SIDE after the player gets a gun
if plr.Backpack:FindFirstChild(tool) == nil then
RemovePlayerToolsSec()
wait(0.1)
onButtonClicked(event, tool, toolPrize, true , "WEAPON")
plr:WaitForChild("Database"):WaitForChild("SecondaryWeaponData").Value = tool
DataSaveEvent:FireServer("SECWPNDTA", tool)
wait(0.1)
secondItem.Text = "Taken"
secondItem.BackgroundColor = BrickColor.new("Baby blue")
else
warn("Player Already owns this weapon")
end
So as a conclusion. The following issue was a result of my own stupidity and illogical coding.