A OnCLick Gui that Gives a Player an Item if they have certain things

Hello everyone. It is me InquistorWasBanned again. I am trying to make a textbutton in a gui that gives a player an item if they have a specific gem and enough currency. If they already have it, they can not buy it again. I think I used the MouseButton function wrong at the end. How can I fix it?

local ClickGui = game.StarterGui.ScreenGui.Frame.ClickButton
local Have = false

local function Check(player)
	
	local backpack = player:WaitForChild("Backpack")
	local Gem = game.ServerStorage.Gem
	local Gold = game.player.leaderstats.Gold
	local MasterSword = game.ServerStorage.Tools.POSword
	
	
	if(backpack:FindFirstChild(Gem.Name)) and Gold.Value >= 100 and Have == false then
		Gold.Value = Gold.Value - 100
		MasterSword:Clone().Parent = backpack
		Have = true
	end
		
end

ClickGui.MouseButton1Click:Connect(Check)
1 Like

Is this a local or server script?

1 Like
local ClickGui = game.StarterGui.ScreenGui.Frame.ClickButton

Why are you using StarterGUI? This should be a local script in the GUI and you should be using remote events to clone to the players backpack and take away currency!

I’m also guessing this is a server script from this line:

local MasterSword = game.ServerStorage.Tools.POSword

What are you trying to achieve here?

	local Gold = game.player.leaderstats.Gold

There is no such thing.

This should be a local script, and when its clicked send a remote event to the server that take away the gold and clones the item in the backpack.

This is a regular script in ServerScriptService.

The gold variable is to name my leader stats to take away from it and check how much they have. So this should be a local script? Also I think maybe I did the MouseClick wrong at the end.

It should be a local script in the GUI and when you press the button, send the gold amount through the remote event to the server and clone the Item from serverstorage to the player’s backpack.

Yes you did, its better to use local scripts for GUIS and doing it through StarterGUI will do nothing as its replicated to PlayerGUI in the player.

If I were you I would parent that server script inside the Gui, so you don’t have to do this:

This is unnecessary. If it is parented, you can just do
local ClickGui = script.Parent.Frame.ClickButton or something similar.

Also, creating a new function is unnecessary as well

It should be

ClickGui.MouseButton1Click:Connect(function()
   -- code here
end)

@KingJoseon Not sure if he should use a local script because he may be changing his values on the server rather than the client.

[EDIT] If you do use a local script you should use events to change it

No, changing the gold on a local script wont change it for everyone else so you must use RemoteEvents to the server.
The local script will handle the button clicking and the serverscript should handle taking away the gold and cloning the item in the backpack.
Edit: Sorry im late

Okay I will try that Thanks So Much!

Please use local scripts for GUIS !

1 Like

Code that is meant to affect a GUI element should always be inside of a LocalScript.

The button click function is used correctly, but it won’t run as long as the code is inside of a standard script (assuming so from the fact that you tried to reference an Instance from ServerStorage, which the Client cannot access).
A possible solution to this would be transferring the code to a LocalScript under StarterGui (preferably as a child of the GUI), and using a RemoteEvent for handling currency changes and tools loadout (since editing these on the Client will, in fact, only change them for the person who bought the item).

Edit: Whoops, I’m late.

2 Likes

Would this work now?


The side shows how I positioned it.

local ClickGui = game.StarterGui.ScreenGui.Frame.ClickButton
local Have = false

local function Check(player)
	
	local backpack = player:WaitForChild("Backpack")
	local Gem = game.ServerStorage.Gem
	local Gold = game.player.leaderstats.Gold
	local MasterSword = game.ServerStorage.Tools.POSword
	
	
	if(backpack:FindFirstChild(Gem.Name)) and Gold.Value >= 100 and Have == false then
		Gold.Value = Gold.Value - 10
		MasterSword:Clone().Parent = backpack
		Have = true
	end
		
end

ClickGui.MouseButton1Click:Connect(Check)

no local scripts cant access storageserver use remote events

Okay let me try. I am not really good at remote events and how to use them. How would I access ServerStorage using a remote event? I know remote events can make it so it works for all players instead of just yourself.

Remote Events are relatively easy, once you know how they work.

  1. Create a RemoteEvent Object. Name it something that is characteristic of what it does.
  2. In your local script. get the RemoteEvent and use the function FireServer(), with the parameters (i.e. the stuff in the parenthesis) being the information from the local script that you need to carry over to the server.
  3. In a server script, you need to listen for the remoteEvent (using remoteEvent.OnServerEvent:connect()) and then create a function that has the same parameters (inputs) as the ones you have for the FireServer() function, and excute what you want it to do (for this instance, modify the gold for that player).
1 Like
local ClickGui = game.StarterGui.ScreenGui.Frame.ClickButton
local Have = false
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorageRemoteEvent = ReplicatedStorage:WaitForChild("ServerStorageEvent")
ServerStorageRemoteEvent:FireServer("ServerStorage")

local function Check(player)
	
	local backpack = player:WaitForChild("Backpack")
	local Gem = game.ServerStorage.Gem
	local Gold = game.player.leaderstats.Gold
	local MasterSword = game.ServerStorage.Tools.POSword
	
	
	if(backpack:FindFirstChild(Gem.Name)) and Gold.Value >= 100 and Have == false then
		Gold.Value = Gold.Value - 10
		MasterSword:Clone().Parent = backpack
		Have = true
	end
		
end

ClickGui.MouseButton1Click:Connect(Check)

Like this? Oh wait I did not do the last part. But did I do the first part correctly? Also I am a little confused on the second part.

No. You need to do this in two scripts, one being the local script (i.e. the script you showed us) and another being a regular script (which you need to create the modify the gold value)

LocalScript:

local ClickGui = game.StarterGui.ScreenGui.Frame.ClickButton
local Have = false
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorageEvent = ReplicatedStorage:WaitForChild("ServerStorageEvent")

local function Check(player)
	
	local backpack = player:WaitForChild("Backpack")
	local Gem = game.ServerStorage.Gem
	local Gold = game.player.leaderstats.Gold
	local MasterSword = game.ServerStorage.Tools.POSword
	
	
	if(backpack:FindFirstChild(Gem.Name)) and Gold.Value >= 100 and Have == false then
        ServerStorageEvent:FireServer(player, Gold, MasterSword)
        Have = true
	end
		
end

ClickGui.MouseButton1Click:Connect(Check)

Regular Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorageEvent = ReplicatedStorage:WaitForChild("ServerStorageEvent")

function giveTool(player, gold, tool)
    gold.Value = gold.Value - 10
    tool:Clone().Parent = player:WaitForChild("Backpack")
end

ServerStorageEvent.OnServerEvent:connect(giveTool)

Modify this if you need to. I haven’t tested this but this is how you should use RemoteEvents.

Oh I see! Thanks! So the regular script is where you deduct and change the gold? There was only one error I found which was you did not define backpack.

Yes, since you want everyone to see that you lost gold, especially for leaderstats

Oops. I’ll fix it real quick. Thank you!

Weird thing is it still does not work.


image
Error at the bottom.