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.)
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?
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
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)
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)