I'm struggling to get item from server storage in to players inventory

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

1 Like

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.

2 Likes

Moving the tools into ReplicatedStorage would help (and tinkering the code a bit to fit that new item path)

But also, is credits a player’s credits in-game? If so you should not check on the client as exploiters can change their credits on the client

also hi scriptking

2 Likes

It is a server script not a local script as it only handles the event fired from player by .OnServerEvent

2 Likes

It is a server script which checks a server sided value. changing value on client does not effect what server sees.

Is there anyway I could see the whole script?

1 Like

Yeah sure, let me boot up studio really quick. 2 minutes

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)
1 Like

I have an idea, switch this

ServerStorage[item]:Clone().Parent = player.Backpack

to this:

ServerStorage[”P90”]:Clone().Parent = player.Backpack

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 :man_shrugging:

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.

1 Like

According to server the player does indeed have mentioned tools. But client does not see them…?
image
Server POV:
image
Player POV:
image

For a split second right now when i was testing the P90 shows on client and then instantly disappears.

Happens to every item in a shop

1 Like

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 :melting_face:

1 Like

Iv been having that problem too recently but i switched from Local script to ServerScript and it worked.

1 Like

I mean, the tools works just fine if you use :give command in admin commands

As I have stated previously shop handler is a server sided script. Also the issue is Server to client not client to server.

2 Likes

Okay, thank you for explaning.

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.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.