Leaderstat question

Yo, i recently made an idea for a tycoon as a starter game for myself because im not too great at developing. However, its been going well and i ran into a little question to ask all of you.

Also what i have that might not be aqs important is that i made a Textlabel that used leaderstats as text for a more appealing visual.

How can you add to a leaderstat when a block is dropped with Instance.New

Here is a few different scripts

dropper script

--// Variables
local Dropper = game.Workspace["Part 4"]
local ProximityPrompt = game.Workspace.proxy.ProximityPrompt
local Triggered = false

ProximityPrompt.Triggered:Connect(function()
	Triggered = true
end)

local function MakeBlock()
	local Block = Instance.new("Part")
	Block.Parent = workspace
	Block.CFrame = Dropper.CFrame
	Block.Size = Vector3.new(2, 1, 2)
	Block.Material = Enum.Material.Concrete
	Block.BrickColor = BrickColor.new("White")
	game.Debris:AddItem(Block, 2)
end

while true do
	if Triggered then
		task.wait(1.5)
		MakeBlock()
	end

	task.wait()
end

leaderstat script


game.Players.PlayerAdded:Connect(function(player)     

	local leaderstats = Instance.new("Folder") 
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money" 
	money.Value = 15 
	money.Parent = leaderstats
	
end)

Basically i just need every new block to create +1 in the leaderstat.
:wink:

1 Like
--// Variables
local Dropper = game.Workspace["Part 4"]
local ProximityPrompt = game.Workspace.proxy.ProximityPrompt
local Triggered = false
local plr = nil;

ProximityPrompt.Triggered:Connect(function(player)
	plr = player
	Triggered = true
end)

local function AddMoney(Amt: number, Plr: Player)
	if not Plr:FindFirstChild('leaderstats') then
		warn("Couldn't find leaderstats for " .. Plr.Name)
		return
	else
		Plr.leaderstats.Money.Value += Amt
	end
end

local function MakeBlock()
	local Block = Instance.new("Part")
	Block.Parent = workspace
	Block.CFrame = Dropper.CFrame
	Block.Size = Vector3.new(2, 1, 2)
	Block.Material = Enum.Material.Concrete
	Block.BrickColor = BrickColor.new("White")
	game.Debris:AddItem(Block, 2)
	AddMoney(1, plr)
	Triggered = false
end

while true do
	if Triggered then
		task.wait(1.5)
		MakeBlock()
	end

	task.wait()
end

There are much better ways to do this, but I wanted to respect your original code as much as possible. Good luck with your project!

local function addToLeaderstat(leaderstat: IntValue | NumberValue, x: number)
	leaderstat.Value += x
end

--a function for fetching the tycoon model/folder by dropper
local function getTycoon(dropper: Instance): Instance
	--I suggest you use FindFirstAncestor
end

--a function for getting the player that owns a dropper
local function getPlayer(dropper: Instance): Player
	local tycoon = getTycoon(dropper)
	--save the player name/userId inside the tycoon so you can locate it
end

function MakeBlock()
	--your code
	Block.BrickColor = BrickColor.new("White")
	addToLeaderstat(getPlayer(Dropper).Money, 1)
	game.Debris:AddItem(Block, 2)
end)

Good news! it worked!
Just one more thing and then im good to leave this topic.
For some reason, when the prompt is triggered, it spawns the dropper and only drops 1 block.
I want the dropper to repeat, and i already tried and saw

while true do

end

Could you show the new current code that you currently have right now?

Of course!

i used IDominusU’s code for dropping block(s) and adding to leaderstats

--// Variables
local Dropper = game.Workspace["Part 4"]
local ProximityPrompt = game.Workspace.proxy.ProximityPrompt
local Triggered = false
local plr = nil;

ProximityPrompt.Triggered:Connect(function(player)
	plr = player
	Triggered = true
end)

local function AddMoney(Amt: number, Plr: Player)
	if not Plr:FindFirstChild('leaderstats') then
		warn("Couldn't find leaderstats for " .. Plr.Name)
		return
	else
		Plr.leaderstats.Money.Value += Amt
	end
end

local function MakeBlock()
	local Block = Instance.new("Part")
	Block.Parent = workspace
	Block.CFrame = Dropper.CFrame
	Block.Size = Vector3.new(2, 1, 2)
	Block.Material = Enum.Material.Concrete
	Block.BrickColor = BrickColor.new("White")
	game.Debris:AddItem(Block, 2)
	AddMoney(1, plr)
	Triggered = false
end

while true do
	if Triggered then
		task.wait(1.5)
		MakeBlock()
	end

	task.wait()
end


! singular block will drop, but i need it to repeatedly drop

Well yeah cause it seems here, you’re making triggered to false, and the statement inside the loop is only being ranned if triggered is set to true, and which you didn’t, it led to just creating 1 block, but the loop is still running.

local function MakeBlock()
	local Block = Instance.new("Part")
	Block.Parent = workspace
	Block.CFrame = Dropper.CFrame
	Block.Size = Vector3.new(2, 1, 2)
	Block.Material = Enum.Material.Concrete
	Block.BrickColor = BrickColor.new("White")
	game.Debris:AddItem(Block, 2)
	AddMoney(1, plr)
	Triggered = false -- Setting it back to false?
end

I turned the trigger to false for my own testing and completely forgot to change that back. My apologies! Delete the Triggered = false within the MakeBlock() function.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.