Need help with crafting script

l placed this local script in a GUI button and it doesn’t seem to give the tool but when I do ReplicatedStorage it does but the problem with that is the knife stops working when it is placed in ReplicatedStorage, how do I make the script take the tool from ServerStorage?

local rs = game:GetService(“ServerStorage”)
local BigMaterKnife = rs:WaitForChild((“Knife - Big Mater”))
local player = game.Players.LocalPlayer
local backpack = player.Backpack

local function craftPressed()
local Ticket = backpack:FindFirstChild(“Big Mater Cinema Ticket”)
local Knife = backpack:FindFirstChild(“Knife”)
if Ticket then
if Knife then
Knife:Destroy()
Ticket:Destroy()
local BigMaterKnifeC = BigMaterKnife:Clone()
BigMaterKnifeC.Parent = backpack
end
end
end

script.Parent.CraftButton.MouseButton1Click:connect(craftPressed)

Localscripts can’t access ServerStorage. Use a remote event to run a server script where the tool gets cloned.

2 Likes

It seems that you are trying to create a script that allows the player to craft a specific knife by consuming a specific item, “Big Mater Cinema Ticket”, in their backpack.

The script you have provided looks correct, with the exception that it is trying to find the “Knife - Big Mater” item in the ServerStorage service instead of the ReplicatedStorage service.

To fix this, you can simply change the following line:

local rs = game:GetService("ServerStorage")

to

local rs = game:GetService("ReplicatedStorage")

This will allow the script to find the “Knife - Big Mater” item in the ReplicatedStorage service and clone it for the player’s backpack.

I hope this helps! Let me know if you have any other questions.

1 Like

This is also correct but I think @CrashAnimates wanted to clone the item while it is parented to the ServerStorage.

To clone an item that is parented to the ServerStorage service, you can use the Clone method on the item itself.

For example, if you have an item called “Knife - Big Mater” that is parented to the ServerStorage service, you can clone it with the following code:

local rs = game:GetService("ServerStorage")
local BigMaterKnife = rs:WaitForChild("Knife - Big Mater")
local player = game.Players.LocalPlayer
local backpack = player.Backpack

local BigMaterKnifeC = BigMaterKnife:Clone()
BigMaterKnifeC.Parent = backpack

This will create a clone of the “Knife - Big Mater” item and parent it to the player’s backpack.

I hope this helps! Let me know if you have any other questions.

You can’t access ServerStorage or ServerScriptService via LocalScript.

That’s correct! In Roblox, the ServerStorage and ServerScriptService services can only be accessed from a script running on the server. This means that you cannot use a LocalScript to access or interact with these services.

If you want to access or interact with items in the ServerStorage service or execute server-side code from a LocalScript , you can use a RemoteFunction to communicate with the server.

Here’s an example of how you can use a RemoteFunction to clone an item from the ServerStorage service and add it to a player’s backpack:

Server-side script:

local rs = game:GetService("ServerStorage")

function giveItem(player, itemName)
	local player = game.Players:GetPlayerByUserId(player)
	local backpack = player.Backpack
	
	local item = rs:WaitForChild(itemName)
	local itemClone = item:Clone()
	itemClone.Parent = backpack
	
	return true
end

Local script:

local player = game.Players.LocalPlayer
local backpack = player.Backpack

local giveItemFunction = game:GetService("ReplicatedStorage"):WaitForChild("giveItem")

local function giveItem(itemName)
	local success = giveItemFunction:InvokeServer(player.UserId, itemName)
	
	if success then
		print(itemName .. " added to backpack!")
	else
		print("Error adding item to backpack")
	end
end

giveItem("Knife - Big Mater")

In this example, the giveItem function is defined in a server-side script and is called using the InvokeServer method on a RemoteFunction object. This allows the client (the player running the LocalScript ) to request that the server execute the giveItem function and provide the necessary arguments.

I hope this helps! Let me know if you have any other questions.

1 Like

You’re going to need a client → server → client remote event.
Client → Server: The local script will tell the server that it needs to take the tool out from serverstorage
Server → Client: the tool gets cloned to the client.

Yes, that’s correct! Using a RemoteEvent to communicate between the client and the server is another way to allow a LocalScript to access or interact with items in the ServerStorage service or execute server-side code.

Server-side script:

local rs = game:GetService("ServerStorage")

game.ReplicatedStorage.giveItem:OnServerEvent:Connect(function(player, itemName)
	local player = game.Players:GetPlayerByUserId(player)
	local backpack = player.Backpack
	
	local item = rs:WaitForChild(itemName)
	local itemClone = item:Clone()
	itemClone.Parent = backpack
end)

Local script:

local player = game.Players.LocalPlayer

game.ReplicatedStorage.giveItem.OnClientEvent:Connect(function(itemName)
	local backpack = player.Backpack
	local item = backpack:FindFirstChild(itemName)
	
	if item then
		print(itemName .. " added to backpack!")
	else
		print("Error adding item to backpack")
	end
end)

game.ReplicatedStorage.giveItem:FireServer(player.UserId, "Knife - Big Mater")

In this example, the client (the player running the LocalScript ) sends a request to the server using the FireServer method on a RemoteEvent object. The server then executes the event’s OnServerEvent connection, which clones the specified item from the ServerStorage service and adds it to the player’s backpack. The server then sends a response to the client using the OnClientEvent connection of the RemoteEvent , allowing the client to confirm that the item was added to the backpack.

I hope this helps! Let me know if you have any other questions.

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