Buyscript not working

Hey.

I’m making a city game, and whenever someone buys a item, the cash gets taxed and part of it goes to the leader. I made a buyscript that’s working, but it doesn’t pay the leader. I don’t get what’s wrong, and I’m pretty much lost. Thanks for helping:

local replicatedstorage = game:GetService("ReplicatedStorage")
local taxes = replicatedstorage:WaitForChild('Taxes')

prompt.Triggered:Connect(function(player)
	local CashData = DataStore2("Cash", player)
	local playerstrn = game.Players:GetPlayerByUserId(stringval.Value)
	local JobData = DataStore2("Cash", playerstrn)
	local totalcash = 10 / taxes.Value
	
	if CashData:Get() >= 10 then
		local leaderpart = totalcash - 10
		if taxes.Value == 0 then
			JobData:Increment(10)
			playerstrn.Hunger.Value += 50
		else
			print(totalcash)
			JobData:Increment(totalcash)
			playerstrn.Hunger.Value += 50
			leader.leaderstats.Cash.Value += leaderpart
		end
	end
	
end)

leavejobevent.OnServerEvent:Connect(function(player)
	player.PlayerGui.JobGui.Enabled = false
	stringval.Value = ""
	player.Character.Humanoid.WalkSpeed = 16
	player.Character.Humanoid.UseJumpPower = true
	player.Character.Humanoid.JumpPower = 50 -- regular jumpspeed is 50
	prompt.Parent.Parent.Prox.ProximityPrompt.Enabled = false
	prompt.Parent.Parent.Job.ProximityPrompt.Enabled = true
end)

(Sorry if the code looks messy, I don’t know how to optimize that much.)

My best piece of advice is jut to start printing terms, and I mean everything. Then you can start to actually see what is happening underneath.

Alright, I got an error:

 Workspace.Model.Prox.ProximityPrompt.Script:32: attempt to index nil with 'Cash' 

How do I fix that?

The instance you placed is not being found, try to see if it is in place with a FindFirstChild

 Workspace.Model.Prox.ProximityPrompt.Script:32: attempt to index nil with 'FindFirstChild' 

Humm, shows that you put on this line 32

Line 32 was this part:

leader.leaderstats.Cash.Value += leaderpart

I added the findfirstchild to it. Didn’t work,

I forgot to add this, but this is how I got the leader variable:

local leader = game.Teams.Leader:GetPlayers()

Try :WaitForChild, it is worth a try

Based on that it looks like leaderstats is nil, would it actually require the owner to be in there?
Because leaderstats are normally set when the player joins, or was your player the leader?

.Script:32: attempt to index nil with 'WaitForChild'

There’s only one person on the leader team, and some of the money goes to that player whenever someone buys a thing.

Try this

local leader = game.Teams.Leader:GetPlayers()
if not (leader:FindFirstChild("leaderstats") == nil) then
	local a1 = leader:WaitForChild("leaderstats")
	if not (a1:FindFirstChild("Cash") == nil) then
		a1:WaitForChild("Cash").Value += leaderpart
	else
		return warn("Cash nil")
	end
else
	return warn("leaderstats nil")
end

Script:29: attempt to call a nil value 

Did I do something wrong? Here’s my script:

local prompt = script.Parent

local leader = game.Teams.Leader:GetPlayers()
local replicatedstorage = game:GetService("ReplicatedStorage")
local leavejobevent = replicatedstorage:WaitForChild("LeaveJob")
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)

local replicatedservice = game:GetService("ReplicatedStorage")
local eatevent = replicatedservice:WaitForChild("Eat")

local stringval = prompt.Parent.Parent.Job.Value
local replicatedstorage = game:GetService("ReplicatedStorage")
local taxes = replicatedstorage:WaitForChild('Taxes')

prompt.Triggered:Connect(function(player)
	local CashData = DataStore2("Cash", player)
	local playerstrn = game.Players:GetPlayerByUserId(stringval.Value)
	local JobData = DataStore2("Cash", playerstrn)
	local totalcash = 10 / taxes.Value
	local leaderpart = totalcash - 10
	
	if CashData:Get() >= 10 then
		
		if taxes.Value == 0 then
			JobData:Increment(10)
			playerstrn.Hunger.Value += 50
		else
			if not (leader:FindFirstChild("leaderstats") == nil) then
				local a1 = leader:WaitForChild("leaderstats")
				if not (a1:FindFirstChild("Cash") == nil) then
					a1:WaitForChild("Cash").Value += leaderpart
					JobData:Increment(totalcash)
					playerstrn.Hunger.Value += 50
				else
					return warn("Cash nil")
				end
			else
				return warn("leaderstats nil")
			end
		end
	end
	
end)

leavejobevent.OnServerEvent:Connect(function(player)
	player.PlayerGui.JobGui.Enabled = false
	stringval.Value = ""
	player.Character.Humanoid.WalkSpeed = 16
	player.Character.Humanoid.UseJumpPower = true
	player.Character.Humanoid.JumpPower = 50 -- regular jumpspeed is 50
	prompt.Parent.Parent.Prox.ProximityPrompt.Enabled = false
	prompt.Parent.Parent.Job.ProximityPrompt.Enabled = true
end)

I’m really confused I tried to explain this to create a leaderstats in a team and I couldn’t, if I can I’ll show you how I did it

I don’t really understand what you mean… I just want to fix this thing where the leader doesn’t get paid.

now I understand what you want to do, here a exemple

local Teams_Service_1 = game:GetService("Teams")
for _, player in next, Teams_Service_1:WaitForChild("leader"):GetPlayers() do
	print(player.Name)
	if not (player:FindFirstChild("leaderstats") == nil) then
		local a3 = player:WaitForChild("leaderstats")
		if not (a3:WaitForChild("Cash") == nil) then
			a3:WaitForChild("Cash").Value += player
		else
			return warn("Cash nil")
		end
	else
		return warn("leaderstats nil")
	end
end

The leader is not getting any money, no errors too.

local prompt = script.Parent

local leader = game.Teams.Leader:GetPlayers()
local replicatedstorage = game:GetService("ReplicatedStorage")
local leavejobevent = replicatedstorage:WaitForChild("LeaveJob")
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)

local replicatedservice = game:GetService("ReplicatedStorage")
local eatevent = replicatedservice:WaitForChild("Eat")

local stringval = prompt.Parent.Parent.Job.Value
local replicatedstorage = game:GetService("ReplicatedStorage")
local taxes = replicatedstorage:WaitForChild('Taxes')

prompt.Triggered:Connect(function(player)
	local CashData = DataStore2("Cash", player)
	local playerstrn = game.Players:GetPlayerByUserId(stringval.Value)
	local JobData = DataStore2("Cash", playerstrn)
	local totalcash = 10 / taxes.Value
	local leaderpart = totalcash / 10
	
	if CashData:Get() >= 10 then
		
		if taxes.Value == 0 then
			JobData:Increment(10)
			playerstrn.Hunger.Value += 50
		else
			local Teams_Service_1 = game:GetService("Teams")
			for _, player in next, Teams_Service_1:WaitForChild("Leader"):GetPlayers() do
				print(player.Name)
				if not (player:FindFirstChild("leaderstats") == nil) then
					local a3 = player:WaitForChild("leaderstats")
					if not (a3:WaitForChild("Cash") == nil) then
						JobData:Increment(totalcash)
						playerstrn.Hunger.Value += 50
						a3:WaitForChild("Cash").Value += leaderpart
					else
						return warn("Cash nil")
					end
				else
					return warn("leaderstats nil")
				end
			end

			print(totalcash)
			JobData:Increment(totalcash)
			playerstrn.Hunger.Value += 50
			for _, lead in pairs(leader) do
				lead:WaitForChild("leaderstats").Cash.Value += leaderpart
			end
		end
	end
	
end)

leavejobevent.OnServerEvent:Connect(function(player)
	player.PlayerGui.JobGui.Enabled = false
	stringval.Value = ""
	player.Character.Humanoid.WalkSpeed = 16
	player.Character.Humanoid.UseJumpPower = true
	player.Character.Humanoid.JumpPower = 50 -- regular jumpspeed is 50
	prompt.Parent.Parent.Prox.ProximityPrompt.Enabled = false
	prompt.Parent.Parent.Job.ProximityPrompt.Enabled = true
end)