Purchasing a Weapon Script

So I finished a long script for my shop in my game. I am making a FPS game and I want players to be able to purchase their own weapons. I made a script and I want to see your guy’s thoughts on my script.

So here is the LocalScript that I have:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	if player.leaderstats.Points.Value >= 550 then -- check to make sure if the player has the amount to pay
		local check = player.PlayerGui.PlayScreen.ShopFrames.CheckToMakeSure -- from line 5 to 19 is to check to make sure
		local checkClone = check:Clone()
		checkClone.Parent = script.Parent.Parent.Parent.Parent.Parent
		checkClone.Visible = true
		checkClone.MainText.Text = "Are you sure you want to buy AK47?"
		checkClone.Price.Text = script.Parent.Parent.Price.Text
		checkClone.ToolImage.Image = script.Parent.Parent.ImageLabel.Image
		script.Parent.Parent.Parent.Parent.Parent.Close.Visible = false

		checkClone.No.MouseButton1Click:Connect(function()
			checkClone:Destroy()
			script.Parent.Parent.Parent.Parent.Parent.Close.Visible = true
		end)

		checkClone.Yes.MouseButton1Click:Connect(function() -- check to make sure page closes
			game.ReplicatedStorage.GunEvents.AK47:FireServer() -- fires a remote script
			player.PlayerGui.PlayScreen.ShopFrames.Purchase:Play() -- plays a money sound
			checkClone:Destroy() -- fully closes check to make sure page
			script.Parent.Parent.Parent.Parent.Parent.Close.Visible = true -- return back the close button on the main menu
			local inventroy = player.PlayerGui.PlayScreen.SelectWeapon.GunsFolder.AK47 -- getting the inventory
			inventroy.Visible = true
			script.Parent.Visible = false
			script.Parent.Parent.Owned.Visible = true
			script.Parent.Parent.Owned.Text = "Purchased!"
			wait(1.5)
			script.Parent.Parent.Owned.Text = "Owned"
			script.Parent:Destroy()
		end)
	else
		player.PlayerGui.PlayScreen.ShopFrames.Denined:Play() -- plays a error sound
		script.Parent.Text = "Not Enought" -- tells the player they do not have enough coins to buy the product.
		script.Parent.BackgroundColor3 = Color3.new(0.985946, 0, 0.0361639)
		wait(1.5)
		script.Parent.Text = "Purchase"
		script.Parent.BackgroundColor3 = Color3.new(0.115312, 0.780041, 0.0152285)
	end
end)

The “Check to make sure UI” that I have is a pop up UI Frame before they purchase the weapon just to make sure that they want to buy the weapon.

Here is my Server Script that I have for the remote when a player purchases the weapon:

game.ReplicatedStorage.GunEvents.AK47.OnServerEvent:Connect(function(player)
	if player.MembershipType == Enum.MembershipType.Premium then
		if player.leaderstats.Points.Value >= 451 then
			local playerProfile = ProfileService[player]
			playerProfile.Data.Points -= 451

			local Points = player:FindFirstChild("leaderstats"):FindFirstChild("Points")
			Points.Value = playerProfile.Data.Points

			if player.myInventory:FindFirstChild("AK47") then
				print("Player Already Owns AK47")
				playerProfile.Data.Points += 451
				
				Points.Value = playerProfile.Data.Points
			else 
				local value = Instance.new("StringValue")
				value.Name = "AK47"
				value.Value = "AK47"
				value.Parent = player.myInventory
			end
			
		end
	elseif player.MembershipType == Enum.MembershipType.None then
		if player.leaderstats.Points.Value >= 550 then
			local playerProfile = ProfileService[player]
			playerProfile.Data.Points -= 550

			local Points = player:FindFirstChild("leaderstats"):FindFirstChild("Points")
			Points.Value = playerProfile.Data.Points

			if player.myInventory:FindFirstChild("AK47") then
				print("Player Already Owns AK47")
				playerProfile.Data.Points += 550

				local Points = player:FindFirstChild("leaderstats"):FindFirstChild("Points")
				Points.Value = playerProfile.Data.Points
			else 
				local value = Instance.new("StringValue")
				value.Name = "AK47"
				value.Value = "AK47"
				value.Parent = player.myInventory
			end
		end
	end
	
	player.PlayerGui.PlayScreen.SelectWeapon.GunsFolder.AK47.Visible = true -- making the weapon visible in the players inventory
end)

I am using ProfileService for this script. I also have a script where if the player does have membership they get a certain percentage off the weapon.

Let me know if you have any questions and thank you for the feedback.

Sincerely,

papahetfan

I have many issues with this script…

These lines don’t work with each other. A Premium member needs 550 points to buy it still, only 451 disappears. In other words a Premium member has anywhere from 451 to 550, they can’t buy it, even at a ‘discounted price’.


Also, Ideally, you wouldn’t want to check the Leaderstats on the Client. Rather shops use Remote Functions and check the Outcome of the result. Heres what I mean:

Here is how your system is ran:

Person clicks Button → Checks they want to buy item by asking “Are you sure” → Both results happen at the same time:
→ Client thinks item is bought and changes Text to Owned
→ Fires Remote event to buy item / Take away currency , All is well.

In the perfect world that should always happen and work smoothly. However there is an Issue:

→ If an exploiter changes their currency on the client, not the Server. The Proccess woun’t be accurate as “Client thinks item is bought and changes Text to Owned”, but on the Server the Item doesn’t pass the “If statements”. (P.S most commonly this is with exploiters, but this issue would also happen if the Server and client value are different. )

What you would want to do generally, is use a remote function and do something like this:

Person clicks Button → Checks they want to buy item by asking “Are you sure” → Client listens for a result on Server; Fires Remote Function → Server does the deciding and buy’s the item and takes away currency. If failed, Server returns the Issue to client. → Client recieves this information, if success, makes text to Owned and all that. If failed, deplays error related to buying item


Also your system seems to only work with one item in the Shop (AK47). Being able to easily add more items to the shop is not something in your system. In otherwords, if you wanted to add 10 more Weapons, you system isn’t adaptable to that. This is something I personally try to achieve in my scripting is making it work so I can add X amount easily no problem. I work with a team of developers, and I’m the only scripter. I make my systems work so my builders/Modelers can add their weapons to the shop without changing the script itself.

For example, I have a Game where there is 3-5 Ais walking around. There is 1 script, and 2 major functions, with other smaller functions as well. to Manage 3 Agents movements. They all work the same, they just have a Folder called “Personalities”. This holds that data that makes each Ai unique and not the same. This was important for me to save and saved me a lot of time!

image

So I would think making your script more adaptive to adding more items, would be nice!

~~ That is my feedback mostly! Let me know if you have any questions

1 Like