How would I do this?

So I wanna make a leaderboard so if you have 50 cash you can touch a part and a gui appears, if you don’t have 50 cash then you can’t touch the part and the gui won’t appear. for anyone wanting to make me a script the this is for you only:

Make a script and make it so there’s a leaderboard and if you have 50 cash. and you touch a part named “Gift” then a gui appears. the gui/TextLabel is called Message
then you get a tool

1 Like

Just listing details but you can work it like this:

A script listens to a touched event for that part, and when the part is touched, you can use game.Players:GetPlayerFromCharacter(touchedpart.Parent) and check if its a player. If it is a player, you can set the gui’s visible property to true after checking if the player.leaderstats.Cash.Value >= 50 returns true.

1 Like

Here is the code for setup, reply or DM me if you have any questions or if you would like me to elaborate:

script.Parent.Touched:Connect(function(hit) --Event fires when the part is tocuhed
	if hit.Parent:FindFirstChild("Humanoid") then --Confirms it is a character
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Finds the player
		if player.leaderstats.Cash.Value >= 50 then --Checks for the right amount, change 'Cash' to the name of your stat
			local clonedUi = game.StarterGui:FindFirstChild("ScreenGui"):Clone() --Change ScreenGui to the Gui name
			clonedUi.Parent = player.PlayerGui --Inserts the Gui into the player so only they can see it
		end
	end
end)

Please note that your ‘Message’ needs to be inside of a screen Gui or else it won’t show up.

1 Like

Whenever a player joins the game, the descendants of game.StarterGui are cloned into player.PlayerGui. The player will just have 2 ScreenGuis if you clone it. Instead, the already cloned Gui in PlayerGui should be Enabled (or you can make the frame or TextLabel inside Visible).

1 Like

I didn’t mean to write game.StarterGui but yes that works as well :slightly_smiling_face:

I also forgot to mention that the player gets a tool, can you do that? let me edit my message and add tool too

Is it a purchase Gui? And when the player clicks confirm they get the tool? If so, this will work:

You need to insert a remote event whether it is in the actual Gui itself or in replicated storage (which is recommended), name the remote event whatever you like, for this example it will be called ‘ToolPurchase’.

image

Then you need to insert a local script and a regular script into your Gui button (text button or image button) and rename them to whatever you like.

image

Once that is done, go into the local script and type out:

script.Parent.MouseButton1Click:Connect(function() --Detects for when a player clicks the Gui
	game.ReplicatedStorage.ToolPurchase:FireServer() --Fires the remote event (change ToolPurchase to the name of the remote event if it is named something different)
end)

Now finally, go into the regular script and type out:

game.ReplicatedStorage.ToolPurchase.OnServerEvent:Connect(function(player) --Runs when the remote event is fired, change 'ToolPurchase' to the name of the remote event if you changed it
	local clonedTool = script.Parent.Tool:Clone() --Clones the tool, change 'Tool' to the name of the tool and change script.Parent if it is in a different location.
	clonedTool.Parent = player.Backpack --Puts it in the player's backpack, 'player' being the parameter automatically passed through which tells us who fired the event.
end)

So it worked but how do I make it so if you have 50 cash and you click it you get the tool? and when u get the tool you lose 50 cash?

then the message goes invisible.

The tool is cloning because we don’t want to give the player the specific tool otherwise nobody else can buy it, plus this script won’t give the player the tool if they reset, that takes a few more lines of code.

To make it take away cash, go into the script in the part and type:

script.Parent.Touched:Connect(function(hit) --Event fires when the part is tocuhed
	if hit.Parent:FindFirstChild("Humanoid") then --Confirms it is a character
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Finds the player
		if player.leaderstats.Cash.Value >= 50 then --Checks for the right amount, change 'Cash' to the name of your stat
			local clonedUi = game.StarterGui:FindFirstChild("ScreenGui"):Clone() --Change ScreenGui to the Gui name
			clonedUi.Parent = player.PlayerGui --Inserts the Gui into the player so only they can see it
            player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 50 --Subtracting the cash
		end
	end
end)

Is it possible to remove 1 tool out their inventory if they have 1 already ?

Do you want it so the player can only buy it once? If so type:

script.Parent.Touched:Connect(function(hit) --Event fires when the part is tocuhed
	if hit.Parent:FindFirstChild("Humanoid") then --Confirms it is a character
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Finds the player
		if player.Backpack:FindFirstChild("Tool") == nil then --Change tool to the name of the tool
			if player.leaderstats.Cash.Value >= 50 then --Checks for the right amount, change 'Cash' to the name of your stat
				local clonedUi = game.StarterGui:FindFirstChild("ScreenGui"):Clone() --Change ScreenGui to the Gui name
				clonedUi.Parent = player.PlayerGui --Inserts the Gui into the player so only they can see it
				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 50 --Subtracting the cash
			end
		end
	end
end)

Okay let me type that in my script

Is it all working now? No errors?

Yup everything is working thanks man!

1 Like

No problem, also I saw someone else suggesting protecting the remote events which is true, I would advise you to look up ways to make sure the client (player) cannot give themselves the tool without purchasing by securing the events.

1 Like