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

Why is this working for One Player but not all?

So I got a Leaderboard that someone made for my Surface Shop GUI but when someone is on the server first they get control in the downsizing the money like when it's at 100 for all players then it's 80 for all players when other players click to purchase a item form the shop it let's them buy unlimited amount of items only until the first player runs out of money...

You will notice in the pictures on what I'm talking about.

http://imgur.com/a/MFfea

Here's the Script to the leaderboard

RequestLeaderboardChange

values = script:WaitForChild("RequestedValues"):getChildren()
playerName = script:WaitForChild("playerName")
--The Value Commit Queue is basically a folder that holds lists of values that need to be updated
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()

ManageLeaderboardScript

values = {{"Tickets",100}}

game.Players.ChildAdded:connect(function(player)
	local leaderboard = Instance.new("Folder")
	leaderboard.Name = player.Name
	for i=1,#values do
		local value = Instance.new("IntValue")
		value.Name = values[i][1]
		value.Value = values[i][2]
		value.Parent = leaderboard
	end
	local local_leaderboard = leaderboard:clone()
	local_leaderboard.Name = "leaderstats"
	leaderboard.Parent = script.Parent.Players
	local_leaderboard.Parent = player
	SSS_Players = script.Parent.Players:getChildren()
end)

game.Players.ChildRemoved:connect(function(player)
	script.Parent.Players[player.Name]:Destroy()
	SSS_Players = script.Parent.Players:getChildren()
end)

game.ServerStorage.ValueCommitQueue.ChildAdded:connect(function(folder)
	wait() --Debounce new additions to value commit queue so that all children of said additions are actually acquired
	local playerFolder = script.Parent.Players:findFirstChild(folder.Name)
	if playerFolder then
		local folderValues = folder:getChildren()
		for i=1, #folderValues do
			local playerValue = playerFolder:findFirstChild(folderValues[i].Name)
			if playerValue then
				playerValue.Value = folderValues[i].Value
			end
		end
	end
	wait()
	folder:Destroy()
end)

And LockLeaderboardScript

while true do
	wait()
	SSS_Players = script.Parent.Parent.Players:getChildren()
	for i=1, #SSS_Players do
		local player = game.Players[SSS_Players[i].Name]
		if player then
			leaderstats = player.leaderstats:getChildren()
			for i=1, #leaderstats do
				leaderstats[i].Value = SSS_Players[i][leaderstats[i].Name].Value
			end
		end
	end
end

I don't know which of these scripts don't recognize the other players as separated instead of together... And no Output comes out I tried so I have no idea what is causing this...

Shop Script

--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 = {
		["Sword"] = {
			["Cost"] = 10;
			["Currency"] = "Tickets";
		},
		["Sword2"] = {
			["Cost"] = 20;
			["Currency"] = "Tickets";
		},
		["Sword3"] = {
			["Cost"] = 30;
			["Currency"] = "Tickets";
		}
	}
	
	--This is kind of a messy implementation, just copy item names over here; facilitates for automatic button generation
	local ItemNames = {"Sword","Sword2","Sword3"}

--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.Weapons: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)

3 comments

0

I don't know who wrote this for you, but they're probably going to have to check this themselves. Pretty much all of this code is excessively overcomplicated.

0

So what I can't fix the leaderboard then?

0

Because the script it self works it's just not letting the purchaser his money instead it's taken away the other players money.

2 answers

The problem probably lies within the shop or wherever you update money in that it will be changing the money for all players rather than just the purchaser. Post them up here and we can see if there is any problems like that

1 comment

0

Updated the Post with the Shop Script included.

A good idea is to use the GetPlayers function instead of PlayerAdded In the leaderboards, you need to do GetChildren Lua is CaSe-SeNsItIvE.

If any of this works do some support please!

3 comments

0

Ah. So which line would that be then?

0

I don't know, but I know that you need to use GetPlayers. Maybe somebody else would help us.

0

Yeah hopefully someone else help us... I have no idea where GetPlayers will go or how to use it o.O

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