Tool Giving GUI not working

I have a localscript with a parent of a textbutton, that when pressed should give them the tool ‘ChocolateCream’ from ServerStorage and remove 5 cash from the leaderstats value, however, it is not giving me the tool and not removing the cash from the player.

Script:

local tool = game.ServerStorage.ChocolateCream
local player = game.Players.LocalPlayer 

script.Parent.MouseButton1Click:Connect(function() 
	tool:Clone() 
	tool:Clone().Parent = player.Backpack 
	player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 5
end)`Preformatted text`
1 Like

The client can’t access ServerStorage, only the server can, consider firing a remote event and passing through the player and tool name.

2 Likes

Do you know why the leaderstats may not be working?

Put it in serverScriptService the script

but wouldn’t that break it because it won’t know what its connecting onto?

what do you mean it wouldnnt know what its connecting onto?

You are editing the stats from a LocalScript, the Server needs to do that

the textbutton (the mouse button click)

How do I go about doing that in a different script?

I have a good method for this. In this situation, you wanna use remote events so that the server script and local script communicate with each other. Do the following:

Insert a remote event in ReplicatedStorage and name it to ChocolateCreamEvent.
In your local script, you want to fire the remote event once the text button is clicked/tapped. Use the following code in your local script, and replace everything that you currently have right now:

local Event = game.ReplicatedStorage.ChocolateCreamEvent
local player = game.Players.LocalPlayer
local Button = script.Parent

Button.Activated:Connect(function()
   if player.leaderstats.Cash.Value < 5 then
      return
   end
   Event:FireServer()
end)

Now, insert a server script inside of ServerScriptService and add the following code inside it:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local Event = ReplicatedStorage:WaitForChild("ChocolateCreamEvent")
local Tool = ServerStorage:WaitForChild("ChocolateCream")

local function BuyTool(player)
   if player:FindFirstChild("leaderstats").Cash.Value < 5 then
      return
   end
   player:FindFirstChild("leaderstats").Cash.Value -= 5
   local CloneTool = Tool:Clone()
   CloneTool.Parent = player.Backpack
end
Event.OnServerEvent:Connect(BuyTool)

You are not meant to send the player through remote events from Client → Server Roblox does that for you already, and you should also check on the Server if their Value is 5, a possible exploiter could just fire this RemoteEvent without the check going through properly

It works completely fine for me, but yea i agree

Hey I tried that, however, it isn’t subtracting the cash value or even giving me the tool anymore.

1 Like

Nevermind, I fixed it up because of a silly mistake. Thank you so much for this!

1 Like