i've lately been receiving this error: attempt to perform arithmetic on field 'Coins' (a nil value) and am not quite sure why
here is the code:
local module = {}
local MarketplaceService = game:GetService("MarketplaceService")
redPlayers = 0
bluePlayers = 0
red = {}
blue ={}
redCount =0
blueCount =0
redTimes = 0
blueTimes = 0
local Rewards = {
-- won
won={
-- first position
{
Xp = 40;
Coins = 45;
wins = 1
};
};
--Lost
lost={
{
Xp = 30;
Coins = 35;
wins = 1
};
};
--Tie
tie={
{
Xp = 5;
Coins = 10;
wins = 0
};
}
};
--[[
game.Serverstorage.maps.RaceObby has a script in it
that lsitens for when a player reaches the end, if a player reaches the end, it calls module.add()
--]]
module.add = function(Team,playerName)
if Team == "red" then
local function playerExist()
for i, v in pairs(red) do
if v == playerName then
return 'Exist'
end
end
end
if playerExist() ~= 'Exist' then
table.insert(red,#red+1,playerName)
print("Doesnt exisit")
--redCount=redCount+1
end
redTimes = redTimes+1
elseif Team=="blue" then
local function playerExist()
for i, v in pairs(blue) do
if v == playerName then
return 'Exist'
end
end
end
if playerExist() ~= "Exist" then
print("Doesnt exisit")
table.insert(blue,#blue+1,playerName)
--blueCount=blueCount+1
end
blueTimes = blueTimes + 1
end
end
--resets the number of players that successfully made it
module.reset = function()
redPlayers=0
bluePlayers=0
--resetting red table
for i, v in pairs(red) do
table.remove(red,i)
-- redCount=redCount-1
end
--resetting blue table
for i, v in pairs(blue) do
table.remove(blue,i)
end
-- redCount=1
-- blueCount=1
redTimes=0
blueTimes=0
end
--[[game.ReplicatedStorage.SystemEvents.ObbyResults.PlayerResults:FireClient(game.Players:FindFirstChild("Arsubia12"),{
Has2xPass =false;
won = true;
position=2
})
]]
local playerInfo = {
Has2X = nil;
Won=nil
}
-- returns the result
module.give = function()
for i, v in pairs(red) do
print(typeof(v).. " red")
end
for i, v in pairs(blue) do
print(typeof(v) .." blue")
end
--game.ReplicatedStorage.SystemEvents.CaptureTheFlag.sendResult:FireAllClients(red,blue, blueTimes, redTimes)
game.ReplicatedStorage.SystemEvents.CaptureTheFlag.sendResult:FireAllClients(red, blue, blueTimes,redTimes)
-- local t=0
-- for i, v in pairs(red) do
-- t=t+0.5
-- end
-- for i, v in pairs(blue) do
-- t=t+0.5
-- end
-- wait(t+5)
--[[ Rewarding players on red team based on wheather they won or not]]
for i, player in pairs(red) do
if game.Players:FindFirstChild(player) then
local plr = game.Players:FindFirstChild(player)
local leaderstats = plr:FindFirstChild('leaderstats')
local xp = plr:FindFirstChild(plr.Name.."'s Xp")
if leaderstats then
if redTimes > blueTimes then
playerInfo.Won=true
if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,5464669) then
leaderstats:FindFirstChild("Coins").Value = leaderstats:FindFirstChils("Coins").Value + (Rewards.won.Coins *2)
xp:FindFirstChild("Xp").Value = (Rewards.won.Xp*2)
playerInfo.Has2X = true
else
leaderstats:FindFirstChild("Coins").Value = leaderstats:FindFirstChils("Coins").Value + (Rewards.won.Coins)
xp:FindFirstChild("Xp").Value = (Rewards.won.Xp)
playerInfo.Has2X = false
end
elseif redTimes < blueTimes then
playerInfo.Won=false
if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,5464669) then
leaderstats:FindFirstChild("Coins").Value = leaderstats:FindFirstChils("Coins").Value + (Rewards.lost.Coins *2)
xp:FindFirstChild("Xp").Value = (Rewards.lost.Xp*2)
playerInfo.Has2X = true
else
leaderstats:FindFirstChild("Coins").Value = leaderstats:FindFirstChils("Coins").Value + (Rewards.lost.Coins)
xp:FindFirstChild("Xp").Value = (Rewards.lost.Xp)
playerInfo.Has2X = false
end
else
playerInfo.Won=nil
if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,5464669) then
leaderstats:FindFirstChild("Coins").Value = leaderstats:FindFirstChils("Coins").Value + (Rewards.tie.Coins *2)
xp:FindFirstChild("Xp").Value = (Rewards.tie.Xp*2)
playerInfo.Has2X = true
else
leaderstats:FindFirstChild("Coins").Value = leaderstats:FindFirstChils("Coins").Value + (Rewards.tie.Coins)
xp:FindFirstChild("Xp").Value = (Rewards.tie.Xp)
playerInfo.Has2X = false
end
end
end
if leaderstats then
game.ReplicatedStorage.SystemEvents.CaptureTheFlag.PlayerResults:FireClient(plr, playerInfo)
end
playerInfo.Has2X=nil
playerInfo.Won=nil
end--FindFirstChild(player)
end; -- for i, v in pairs(red) do
------------------------------------------------------------------------------------------------------
for i, player in pairs(blue) do
if game.Players:FindFirstChild(player) then
local plr = game.Players:FindFirstChild(player)
local leaderstats = plr:FindFirstChild('leaderstats')
local xp = plr:FindFirstChild(plr.Name.."'s Xp")
if leaderstats then
if redTimes > blueTimes then
playerInfo.Won=true
if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,5464669) then
leaderstats:FindFirstChild("Coins").Value = leaderstats:FindFirstChild("Coins").Value + (Rewards.won.Coins *2)
xp:FindFirstChild("Xp").Value = (Rewards.won.Xp*2)
playerInfo.Has2X = true
else
leaderstats:FindFirstChild("Coins").Value = leaderstats:FindFirstChild("Coins").Value + (Rewards.won.Coins)
xp:FindFirstChild("Xp").Value = (Rewards.won.Xp)
playerInfo.Has2X = false
end
elseif redTimes < blueTimes then
playerInfo.Won=false
if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,5464669) then
leaderstats:FindFirstChild("Coins").Value = leaderstats:FindFirstChils("Coins").Value + (Rewards.lost.Coins *2)
xp:FindFirstChild("Xp").Value = (Rewards.lost.Xp*2)
playerInfo.Has2X = true
else
leaderstats:FindFirstChild("Coins").Value = leaderstats:FindFirstChild("Coins").Value + (Rewards.lost.Coins)
xp:FindFirstChild("Xp").Value = (Rewards.lost.Xp)
playerInfo.Has2X = false
end
else
playerInfo.Won=nil
if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,5464669) then
leaderstats:FindFirstChild("Coins").Value = leaderstats:FindFirstChild("Coins").Value + (Rewards.tie.Coins *2)
xp:FindFirstChild("Xp").Value = (Rewards.tie.Xp*2)
playerInfo.Has2X = true
else
leaderstats:FindFirstChild("Coins").Value = leaderstats:FindFirstChild("Coins").Value + (Rewards.tie.Coins)
xp:FindFirstChild("Xp").Value = (Rewards.tie.Xp)
playerInfo.Has2X = false
end
end
end
if leaderstats then
game.ReplicatedStorage.SystemEvents.CaptureTheFlag.PlayerResults:FireClient(plr, playerInfo)
end
playerInfo.Has2X=nil
playerInfo.Won=nil
end--FindFirstChild(player)
end; -- for i, v in pairs(red) do
module.reset()
end;-- function
return module

2 comments
Please state in which line the error occurs, and i don't see Rewards anywhere
You really need to tell us where this error is occuring, we're not going to look through 251 lines of your code just to find the error before we attempt to fix it.