Figure out what's the problem with a section in this ShopScript but don't know how to fix it... The Script works but keeps the first purchaser linked to other players as there money stays the same. Note this script was made by someone else just trying to fix a error with Purchasing... Also it does not Show up with any errors... I know I asked for help on this script before But I'm really trying to fix it my self but I tried everything I could think of. I tried WaitForChild and FindFirstChild but either does not work... So if someone can find out what is wrong that be great!
The Section that is not fully working
--Check if the value we want to check exists in leaderstats clientside; if it does, return it
local function GetMoney(Currency)
local IntValue = LeaderStats:FindFirstChild(Currency)
if IntValue then
return IntValue.Value
end
return 0
end
local function SubtractMoney(Currency, Amount)
--Create a new instance of a leaderboard-change object, initialize it with appropriate values, and commit it to the update queue
--More information on how this works in the RequestLeaderboardChange script in ReplicatedStorage
local LeaderboardChangeObject = RequestLeaderboardChangeMaster:clone()
local valueToUpdate = Instance.new("IntValue")
valueToUpdate.Name = Currency
valueToUpdate.Value = GetMoney(Currency) - Amount
valueToUpdate.Parent = LeaderboardChangeObject.RequestedValues
LeaderboardChangeObject.playerName.Value = Player.Name
LeaderboardChangeObject.Parent = game.Workspace
end
The Main Script with the Section included... This script is in StarterPack so I don't know if that has something to do with the purchase not fully working?
--Services--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--Variables--
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local LeaderStats = Player:FindFirstChild("leaderstats")
local RequestLeaderboardChangeMaster = game.ReplicatedStorage.RequestLeaderboardChange
local Backpack = Player:FindFirstChild("Backpack")
local StarterGear = Player:FindFirstChild("StarterGear")
local SurfaceGui = ReplicatedStorage:FindFirstChild("Shop", true):Clone() --Why bother making changes to the master copy?
SurfaceGui.Parent = PlayerGui
local mouse = Player:GetMouse() --Mouse can be grabbed via localscript; we'll be using this to make shops mouse-over-able for activation
--Define items as an array of arrays; each individual array is an item with name, cost, and currency type specified
local ItemsForSale = {
["Blade"] = {
["Cost"] = 10;
["Currency"] = "Tickets";
},
["Blade2"] = {
["Cost"] = 20;
["Currency"] = "Tickets";
},
["Blade3"] = {
["Cost"] = 30;
["Currency"] = "Tickets";
}
}
--This is kind of a messy implementation, just copy item names over here; facilitates for automatic button generation
local ItemNames = {"Blade","Blade2","Blade3"}
--This maps the Shop GUI onto whatever the mouse is hovering on (provided it's a part named "Shop")
local function UpdateShopTarget()
if mouse.Target then
if mouse.Target.Name == "Shop" then
SurfaceGui.Adornee = mouse.Target
else
SurfaceGui.Adornee = nil
end
else
SurfaceGui.Adornee = nil
end
end
--Check if the value we want to check exists in leaderstats clientside; if it does, return it
local function GetMoney(Currency)
local IntValue = LeaderStats:FindFirstChild(Currency)
if IntValue then
return IntValue.Value
end
return 0
end
local function SubtractMoney(Currency, Amount)
--Create a new instance of a leaderboard-change object, initialize it with appropriate values, and commit it to the update queue
--More information on how this works in the RequestLeaderboardChange script in ReplicatedStorage
local LeaderboardChangeObject = RequestLeaderboardChangeMaster:clone()
local valueToUpdate = Instance.new("IntValue")
valueToUpdate.Name = Currency
valueToUpdate.Value = GetMoney(Currency) - Amount
valueToUpdate.Parent = LeaderboardChangeObject.RequestedValues
LeaderboardChangeObject.playerName.Value = Player.Name
LeaderboardChangeObject.Parent = game.Workspace
end
local function AwardItem(ItemName)
--If the item exists in ReplicatedStorage, clone that sucker right into the player's backpack (and also their StarterGear)
local Item = ReplicatedStorage:FindFirstChild(ItemName)
if Item then
Item = Item:Clone()
Item.Parent = Backpack
--If you want your players to lose their purchases when they die then comment out the next two lines
Item = Item:Clone()
Item.Parent = StarterGear
end
end
--SurfaceGui Pre-initialization--
local Window = SurfaceGui:FindFirstChild("Window")
local ProtoButton = SurfaceGui:FindFirstChild("ProtoButton")
for i=1, #ItemNames do
--This basically just creates a new button for each item you specified earlier so that you don't have to make the whole GUI by hand.
--Seriously, who wants to do that? GUIs suck.
newButton = ProtoButton:clone()
newButton.Name = ItemNames[i]
newButton.Position = UDim2.new(0,22,0,100*i)
newButton.Active = true
newButton.Visible = true
newButton.Parent = Window
end
--Automatically adjusts the window's size to compensate for how many buttons there are
Window.CanvasSize = UDim2.new(1,0,0,100+100*#ItemNames)
--SurfaceGui Variables--
local ArrayOfChildren = Window:GetChildren()
--Didn't really muck with this code much, it's pretty stable
--Basically checks if an item that the button says exists, exists
--If it does exist, check that the button is clicked using object.event:connect()
--If leaderstats exists (it should) and the player has enough money (we can check this player-side because we're not tampering with it), then:
--Call SubtractMoney, which handles the creation of a new RequestLeaderboardChange object and then call AwardItem to give player their shinies
--I commented out the print statements because if you've got more than like 2 items the console spam is VERY ANNOYING.
for _, Button in pairs (ArrayOfChildren) do
local ItemName = Button.Name
--print("Got button", ItemName)
local ItemAttributes = ItemsForSale[ItemName]
if ItemAttributes ~= nil then
--print("Got data for", ItemName)
local Cost = ItemAttributes["Cost"]
local Currency = ItemAttributes["Currency"]
--print(" ", "Cost:", Cost)
--print(" ", "Currency:", Currency)
Button.Text = ItemName..": "..Cost.." "..Currency
Button.MouseButton1Click:connect(function()
if LeaderStats then
if GetMoney(Currency) >= Cost then
SubtractMoney(Currency, Cost)
AwardItem(ItemName)
end
end
end)
end
end
--Connection event to map the Shop GUI onto whatever the mouse is hovering on (provided it's named "Shop")
mouse.Move:connect(UpdateShopTarget)
Here's the RequestLeaderboardChange Script.
values = script:WaitForChild("RequestedValues"):getChildren()
playerName = script.playerName
queue = game.ServerStorage.ValueCommitQueue
playerFolder = Instance.new("Folder")
playerFolder.Name = playerName.Value
playerFolder.Parent = queue
--Move values to queue
for i=1,#values do
values[i].Parent = playerFolder
end
wait()
script:Destroy()
