Script: add to inventory does not work?

Hi
Hi, I’m Sven and I made a Shop GUI.
So I’m making a script which makes the item add to a players inventory if they bought the item: if they have 1 star (the leaderstat, see it as coins)

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
if player.leaderstats.Stars.Value >= 1 then
	
game.ServerStorage.ShopItems.FluffyUnicorn.Clone().Parent = game.player.backpack
		
	end
end)

I have searched a lot (even youtube videos) but it can’t answer my question.
I’m not really good at scripting yet, but I hope someone can tell me what is wrong about my script.
Thank you for reading!

Note: this is a normal script inside of a textbutton
I made a folder named ShopItems inside of the ServerStorage with the item: FluffyUnicorn inside.

This is a local script right? Only the server can view the ServerStorage, so you should put the folder inside of ReplicatedStorage and clone it from there.

I did this in a normal script, but I will change it into a localscript & put the folder inside of the ReplicatedStorage. Will the issue be solved then?

But then that would only clone on the client, so use remote events to get a script to move it :slight_smile:

Players.LocalPlayer is nil on the server. Use a client script for client input.

No keep it as a normal script. But you can’t use game.Players.LocalPlayer in a normal script.

1 Like

they is interacting with with UI objects, it has to be client

1 Like

I do know how to fire a remoteevent, but what should I do then, lol?

Send the tool to the Client and parent it to the backpack I think

Put a Remote Event in ReplicatedStorage

local script

local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("RemoteEvent")

script.Parent.MouseButton1Click:Connect(function()
  if player.leaderstats.Stars.Value >= 1 then 
    Event:FireServer()
  end
end)

script

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("RemoteEvent")

Event.OnServerEvent:Connect(function(player)
  ServerStorage.ShopItems.FluffyUnicorn:Clone().Parent = player.Backpack
end)

Do the Stars check on the server as the client can easily modify the value.

1 Like

Here is what your going to do

Local script:

script.Parent.Activated:Connect(function()
local Event = Instance.new("RemoteEvent",game:GetService("ReplicatedStorage"))
Event:FireServer()
end)

Server Program

game:GetService("ReplicatedStorage").RemoteEvent.OnServerEvent:Connect(function(player)
if player:FindFirstChild("leaderstats") and player:FindFirstChild("leaderstats").Stars.Value >= 1 then
game.ServerStorage.ShopItems.FluffyUnicorn.Clone().Parent = player.backpack
end
end)

May be some errors was written on mobile lol

I will try this, thanks for your help, I appreciate everyone helping.

Its a bit advanced for him yet, Tbh I suggest for him to learn more of the basics

Ah yes OnClientEvent on the server

Like I said I wrote on mobile lmao change it to OnServerEvent

1 Like

lol thank you I will test things now

So it already creates a remoteevent in the script right?

No just insert one into ReplicatedStorage called RemoteEvent

yep it does, if you want to name it add this to the already created script. Ye it makes it go to Replicated storage

local Event = Instance.new("RemoteEvent",game:GetService("ReplicatedStorage")); Event .Name = "YourNameHere"