I assume you're using a local script that is a descendant of StarterGui. Place the "Spaghetti" tool from ServerStorage to ReplicatedStorage because the Client (where local scripts run) cannot access ServerStorage because judging from its name, it can only be accessed by the Server. The Server is the game itself, and only normal scripts can run in the Server-side.
The reason why you place it in ReplicatedStorage is because it can be accessed both the Server and the Client.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local gui = script.Parent.Parent.Parent
local money = ReplicatedStorage:WaitForChild("Money")
local itemToClone = ReplicatedStorage:WaitForChild("Spaghetti")
local player = Players.LocalPlayer
local button = script.Parent
button.MouseButton1Click:Connect(function()
if money.Value >= 50 then
local clonedItem = itemToClone:Clone()
clonedItem.Parent = player.Backpack
money.Value -= 50
script.Click:Play()
end
end)
But this still isn't the final asnwer..
I recommend using RemoteEvents because this script can be an advantage for hackers by changing the Money value in their client, so create a RemoteEvent inside ReplicatedStorage and name it CloneTool, and make a normal script inside ServerScriptService
Your local script in the button should look like this:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local gui = script.Parent.Parent.Parent
local removeEvent = ReplicatedStorage:WaitForChild("CloneTool")
local player = Players.LocalPlayer
local button = script.Parent
local clickSound = script.Click
button.MouseButton1Click:Connect(function()
task.spawn(clickSound.Play, clickSound)
remoteEvent:FireServer()
end)
And your normal script in ServerScriptService:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local removeEvent = ReplicatedStorage.CloneTool
local itemToClone = ReplicatedStorage.Spaghetti
local money = ReplicatedStorage.Money
remoteEvent.OnServerEvent:Connect(function(player)
if money.Value >= 50 then
local clonedItem = itemToClone:Clone()
clonedItem.Parent = player.Backpack
money.Value -= 50
end
end)
BUT THIS STILL ISN'T THE FINAL ANSWER!!
I recommend placing the Money value to each player instead because it can be changed by any player (and even the Server) if it's in ReplicatedStorage. So if you have any scripts that use ReplicatedStorage.Money YOU BETTER CHANGE THEM!
Final normal script in ServerScriptService:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local removeEvent = ReplicatedStorage.CloneTool
local itemToClone = ReplicatedStorage.Spaghetti
Players.PlayerAdded:Connect(function(player)
local money = Instance.new("IntValue", player)
money.Name = "Money"
end)
remoteEvent.OnServerEvent:Connect(function(player)
if player.Money.Value >= 50 then
local clonedItem = itemToClone:Clone()
clonedItem.Parent = player.Backpack
player.Money.Value -= 50
end
end)
sorry for long answer :P
2 comments
Any errors in the console?
You may want to give the player in question the tool on characteradded.