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,