Player not getting tool

I have modified some stuff from the store of my game, i can’t remember what i modified and the “Perks” store got broken, here are the scripts:

--Fire event
wait(1)
local player = game.Players.LocalPlayer
for i,Buttons in pairs(script.Parent.PerksFrame:GetChildren()) do
							local ImageButton = Buttons:FindFirstChild("ImageButton")
	if ImageButton then
			ImageButton.MouseButton1Down:connect(function()
					local HasBought = Buttons:FindFirstChild("HasBought")
					if HasBought then
						if HasBought.Value == true then
							game.ReplicatedStorage.AddGearFromStore:FireServer(Buttons.Name)
						else
		local Cost = Buttons:FindFirstChild("Cost")
		if Cost then
			local leaderstats = player:FindFirstChild("StatsValues")
			if leaderstats then
				local Cash = leaderstats:FindFirstChild("Nubits")
				if Cash then
				if Cash.Value >= Cost.Value then
					HasBought.Value = true
					game.ReplicatedStorage.AddGearFromStore:FireServer(Buttons.Name)
			    game.ReplicatedStorage.SubCash:FireServer(Cost.Value)
				end
				end
			end
		end	
		end
						end
	end)
	end
end
--Give player tool
function onwin(player,Tool)

local Gear = game.Lighting.Gameitems:FindFirstChild(Tool)

if Gear then
	local Backpack = player:FindFirstChild("Backpack")
	local StarterGear = player:FindFirstChild("StarterGear")
	if StarterGear or Backpack or StarterGear and Backpack then
		for i,clearStoreItems in pairs(StarterGear:GetChildren()) do
			local Tool = game.Lighting.Gameitems:FindFirstChild(clearStoreItems.Name)
			if Tool then
				clearStoreItems:Destroy()
			end
		end
		for i,clearStoreItems in pairs(Backpack:GetChildren()) do
			local Tool = game.Lighting.Gameitems:FindFirstChild(clearStoreItems.Name)
			if Tool then
				clearStoreItems:Destroy()
			end
		end
		local char = game.Workspace:FindFirstChild(player.Name)
		if char then
			for i,clearStoreItems in pairs(char:GetChildren()) do
				if clearStoreItems.ClassName == "Tool" then
			local Tool = game.Lighting.Gameitems:FindFirstChild(clearStoreItems.Name)
			if Tool then
				clearStoreItems:Destroy()
			end
			end
			end
			end
		local findif = StarterGear:FindFirstChild(Gear.Name)
		if findif then
			else
		Gear:Clone().Parent = StarterGear
		Gear:Clone().Parent = Backpack
		end
		local OwnedStoreItems = player:FindFirstChild("OwnedStoreItems")
		if OwnedStoreItems then
			local Findif = OwnedStoreItems:FindFirstChild(Gear.Name)
			if Findif then
			else
				local newitem = Instance.new("Folder",OwnedStoreItems)
                newitem.Name = Gear.Name				
			end
		end
	end
end
end
game.ReplicatedStorage.AddGearFromStore.OnServerEvent:connect(onwin)

Sorry by the script being to big, i didn’t made this script, and i also can’t understand this, and you can take your time to see the script and tell me what is wrong, i repeat, take the time to see this, cause this script is very long. Thanks :+1:

3 Likes

Have you got an old copy somewhere you can refer to?

old copy of the script?

30chars

Yes so you can compare the latest with one before you started this particular change.

function onwin(player,Tool)
local Gear = game.Lighting.Gameitems:FindFirstChild(Tool)
if Gear then
	local Backpack = player:FindFirstChild("Backpack")
	local StarterGear = player:FindFirstChild("StarterGear")
	if StarterGear and Backpack then
		for i,clearStoreItems in pairs(StarterGear:GetChildren()) do
			local Tool = game.Lighting.Gameitems:FindFirstChild(clearStoreItems.Name)
			if Tool then
				clearStoreItems:Destroy()
			end
		end
		for i,clearStoreItems in pairs(Backpack:GetChildren()) do
			local Tool = game.Lighting.Gameitems:FindFirstChild(clearStoreItems.Name)
			if Tool then
				clearStoreItems:Destroy()
			end
		end
		local char = game.Workspace:FindFirstChild(player.Name)
		if char then
			for i,clearStoreItems in pairs(char:GetChildren()) do
				if clearStoreItems.ClassName == "Tool" then
			local Tool = game.Lighting.Gameitems:FindFirstChild(clearStoreItems.Name)
			if Tool then
				clearStoreItems:Destroy()
			end
			end
			end
			end
		local findif = StarterGear:FindFirstChild(Gear.Name)
		if findif then
			else
		Gear:Clone().Parent = StarterGear
		Gear:Clone().Parent = Backpack
		end
		local OwnedStoreItems = player:FindFirstChild("OwnedStoreItems")
		if OwnedStoreItems then
			local Findif = OwnedStoreItems:FindFirstChild(Gear.Name)
			if Findif then
			else
				local newitem = Instance.new("Folder",OwnedStoreItems)
                newitem.Name = Gear.Name				
			end
		end
	end
end
end
game.ReplicatedStorage.AddGearFromStore.OnServerEvent:connect(onwin)
wait(1)
local player = game.Players.LocalPlayer
for i,Buttons in pairs(script.Parent.PerksFrame:GetChildren()) do
							local ImageButton = Buttons:FindFirstChild("ImageButton")
	if ImageButton then
			ImageButton.MouseButton1Down:connect(function()
					local HasBought = Buttons:FindFirstChild("HasBought")
					if HasBought then
						if HasBought.Value == true then
							game.ReplicatedStorage.AddGearFromStore:FireServer(Buttons.Name)
						else
		local Cost = Buttons:FindFirstChild("Cost")
		if Cost then
			local leaderstats = player:FindFirstChild("leaderstats")
			if leaderstats then
				local Cash = leaderstats:FindFirstChild("Cash")
				if Cash then
				if Cash.Value >= Cost.Value then
					HasBought.Value = true
					game.ReplicatedStorage.AddGearFromStore:FireServer(Buttons.Name)
			    game.ReplicatedStorage.SubCash:FireServer(Cost.Value)
				end
				end
			end
		end	
		end
						end
	end)
	end
end

Maybe im blind but the only difference that i see from both scripts is that i just change leaderstats to the new carpet for the player stats (StatsValues) and Cash to Nubits.

Ok, so if the change is not obvious then lets just try and fix the latest one.
We can start that by trying to understand what is happening and where.
In both the local script and the server script can you put prints in to create a process path that we can follow?
for example do you get an ImageButton and a Tool where the code expects them?

1 Like

Try changing this to
if StarterGear and Backpack then

and see if it solves your issue.

@astra_wr i did that and still not works
@RamJoT i used prints and this is what i did:

				print("yet")
				local newitem = Instance.new("Folder",OwnedStoreItems)
                newitem.Name = Gear.Name	
print("eyahd")			
			end
		end
	end
end
end
game.ReplicatedStorage.AddGearFromStore.OnServerEvent:connect(onwin)
print("no")

So the output the “no” doesn’t prints, why that happens?

Giving the script to you guys find the issue may take too long. But i will ask you, want the script?

Also, do you know a shop kit? Cause i need it for my game, thanks

Is there any error in the output? Do this just in case:

game:GetService("ReplicatedStorage"):FindFirstChild("AddGearFromStore").OnServerEvent:Connect(onwin)

No, when there’s an error im always trying to fix it or share it here.
But i will use the line you said

Basically you want to give players a tool for every item in the shop but don’t want there to be so many scripts ?

* reply quick please

No, in the shop there are 4 categories, everything works ok but the perks category (Tools) don’t work.

So you basically want there to be a script that gives players (tools to buy ) for every button after you click it right ?
Like a buy script, if he has cash then give him the tool , and let him purchase it, if he doesnt then end the function

Yeah that’s right, but when i click the button, the button text should change to “Owned!” and give you the tool, but now only the text says “Owned!” and you dont get the tool

Working Gui Shop, for multiple items ofcourse

You should keep your tools, parts etc. in ServerStorage

Why , you ask?

Because contents of the ServerStorage are only accessible by the Server itself , the contents are not replicated across the Client , meaning you can store many objects there until they are needed , as network traffic will not be used up transmitting these objects to the client when they join the game, you can use this for events . (However Replicated Storage should be used for something that will have to be called for by a Local Script, Using Lighting to store objects in NOT recommended !

Click this

“Prior to the introduction of ServerStorage and ReplicatedStorage lighting was used for storage. This behavior is not supported and should not be used in new work.”

Now , look at what I came up with, there is detailed documentation in the script I created, do whatever it says and it should definitely work, yes it has several checks i.e whether he already has the tool, whether he has enough cash, again, just read the documentation I wrote in the script.

First a local script parented to the gui, it should have buttons for purchase parented to the “button_container” in the script (read the documentation)

--[[
	I basically scripted the entire thing (XxELECTROFUSIONxX)and it's just a shop gui script thing, probably not the best but it totally works
  So what you will need is :
 *   A frame or anything containing clickable purchase buttons DIRECTLY parented to it, and
 * An int value called amt (amount) that has the price of the object in it, change this for every button
   and this value should exist in every button that would give an item upon purchase, 
 * Next you will need a leaderboard (folder called leaderstats in this case) directly parented to the player,
 * and an int value called Cash parented to the folder,
 * Finally, remember to keep each button's NAME SAME as the name of the tool it will give	, and create a folder
    called Tools in ServerStorage, that ofcourse, will contain the tools
	
                                                                                                                             ]]
local buttons_container = script.Parent.Frame.ScrollingFrame--it can be anything that hasall purchase buttons directly parented to it
local event = game.ReplicatedStorage.Event--get an event "Event"
local players = game:GetService("Players")
local player = players.LocalPlayer

for _, Button in ipairs(buttons_container:GetChildren()) do--loop through em
        if Button:IsA("ImageButton") then --make sure it's a button , you can change it to TextButton if they're text buttons
	Button.MouseButton1Click:Connect(function()
           --for every thing that's an ImageButton do this
	print(player.Name .. "has" ..player.leaderstats.Cash.Value .."Cash")--assuming leaderstats /folder parented to player called "leaderstats" exists and has the value "Cash"
	if player.leaderstats.Cash.Value<Button.amt.Value then --he doesnt have enough cash, do something here..
		 return print("not enough cash")
		
		end
		 --Now that we know he has enough cash, let's see whether he already has the tool !
			if player.Backpack:FindFirstChild(tostring(Button.Name)) then  -- we dont need button.Name(instance) we only need the string.
			return print("you already OWN ".. tostring(Button.Name))--This guy owns the tool , so we just end it, if you'd like though you could play an error sound or something .
			
			 end
		--FIRE THE EVENT, he has cash and wants to buy an item
		event:FireServer(Button.Name)--remember , button.Name for each button is the data we are sending to the server, so it should be the same as the tool's name, 
	       --remember to subtract his cash as he bought the thing
		    player.leaderstats.Cash.Value = player.leaderstats.Cash.Value-Button.amt.Value   
		script.Parent.Purchase:Play()
		  print("fired")
	     end)
      end     
  end

Next , something that runs when the event is fired , a ServerScript in ServerScriptService :

local Event = game.ReplicatedStorage.Event
Event.OnServerEvent:Connect(function(Player, ToolName)--we get the name that was send to us
	if type(ToolName) ~= "string" then
		
		return
	end
	--We already checked in the local script whether he has the tool or not but that was too run a local function on that client , now we do the real thing
if Player.Backpack:FindFirstChild(ToolName) then 
     
	
	return print("you already have" .. ToolName)


end
	
	--remove this line (clear children etc.) if you dont want to reset his inventory each time he buys something new !
	Player.Backpack:ClearAllChildren() wait(0.1)
	
	     --Now we find the tool , from the name that was sent to us, in the folder
	if game.ServerStorage.Tools:FindFirstChild(ToolName) then
		print("Tool" .. ToolName.. "found !")--alright hold on, 
		game.ServerStorage.Tools[ToolName]:Clone().Parent = Player.Backpack
		print("cloned")--He got it ! we're done(for every button all this will happen to return a tool and not if he doesnt have enough cash)
	end
end)

DM me if something does not work, though it should or if you require further assistance !

Might contain errors though not likely, possible improvements might exist in terms of efficiency . (might)

4 Likes

It worked! Well, when i get some problems or a feature that i need, i will message you :+1:

1 Like