Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I check if a player is a certain level to unlock a weapon?

Hello! I'm trying to make a shop gui. Here's what I want to happen:

  1. When the player opens the shop, they can see the swords available and click "Unlock" on the sword they want.

  2. It'll check their level, and if they have the required level, itll change the button text to "Unlocked!" and give them the weapon in their backpack, and give it to them in their StarterGear so they get it when they respawn, and don't have to click the button again.

  3. If their level requirement isn't met, it'll tell them "Insufficient level" and then change back to the old button name.

Here is the GUI: https://imgur.com/a/t0dfhUZ

And here is my code:

local player = game.Players.LocalPlayer
local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
local level = player.leaderstats.Level
tool = game.ReplicatedStorage.Shop_Items.Cutlass

local db = true  --debounce

local price = 1  --required level for item

function buy()
	db = false
	if level.Value >= price then
		local a = tool:clone()
		a.Parent = player.Backpack
		local b = tool:clone()
		b.Parent = player.StarterGear
		script.Parent.Text = ("Unlocked!")
	else
		script.Parent.Text = ("Insufficient Level!")
		wait(3)
		script.Parent.Text = ("Unlock (Lvl 5)")
	end
end
script.Parent.MouseButton1Down:connect(buy)

The script changes the name to Unlocked, and gives the player the sword as I want. HOWEVER:

  1. When the player dies it changes it back to the the previous name, "Unlock (Lvl 5)" and doesn't keep it at "Unlocked!"

  2. The player doesn't spawn with the new sword, even though its added to StarterGear.

  3. The player can click it multiple times, How do I make it so the player can only click the button once per life?

  4. This also creates many duplicate items in the StarterGear folder, (if I spam the button there's tons of swords there.) How can I remove duplicate items from StarterGear?

How could I fix these issues? Thank you very much for any help!

1 answer

When the player dies it changes it back to the the previous name, "Unlock (Lvl 5)" and doesn't keep it at "Unlocked!"

-- Try seeing if the "ResetOnSpawn" property is on in the ScreenGui

The player doesn't spawn with the new sword, even though its added to StarterGear.

-- Try putting the tool in StarterGear from Server, with a normal Script, as it is show here https://developer.roblox.com/en-us/api-reference/class/StarterGear. Or is possible that you will need to enable gears in your game for this to work. Check it out.

The player can click it multiple times, How do I make it so the player can only click the button once per life?

-- You have not checked if "db" var is true or false in the script. Sinse this is a local script, this db variable will be set to true when player respawns. If you dont want the player to buy it ever again you will need to use Datastores.

This also creates many duplicate items in the StarterGear folder, (if I spam the button there's tons of swords there.) How can I remove duplicate items from StarterGear?

-- if not StarterGear:FindFirstChild("GearName") then addgear() end

This discussion is preserved as it was. New answers, comments, and votes are closed.