Im trying to make it so that when someone uses this command every player on the team said gets an certain amount of exp. But on line 16, I get an error that says “attempt to index nil with Exp”
local GroupId = 7458240
local MinimumRankToUseCommand = 10 -- change to high-ranking staff rank
game.Players.PlayerAdded:Connect(function(Player)
if Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommand then
Player.Chatted:Connect(function(Message)
local Words = string.split(Message, " ")
if Words[1] == "c!awardteamexp" then
local NameOfTeamToGivePoint = Words[2]
local AmountOfPointsToGive = Words[3]
local peopleOnTeamList = game.Teams:FindFirstChild(NameOfTeamToGivePoint):GetPlayers()
if NameOfTeamToGivePoint then
peopleOnTeamList.leaderstats.Exp.Value = peopleOnTeamList.leaderstats.Exp.Value + AmountOfPointsToGive
end
end
end)
end
end)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PointsStats")
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Currency = Instance.new("IntValue", Leaderstats)
Currency.Name = "Exp"
Currency.Value = 0
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Currency.Value = Data
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, Player.leaderstats.Exp.Value)
end)
e
The code does not work because you are trying to index (set the value of) something equal to nil.
e.g nil = 2
This happens because on line 16 you index peopleOnTeamList.leaderstats which is equal to nil and then attempt to set its value.
Instead you should be looping through all the people in the table and changing their exp
if Words[1] == "c!awardteamexp" then
local NameOfTeamToGivePoint = Words[2]
local AmountOfPointsToGive = Words[3]
if NameOfTeamToGivePoint then
for i,player in game.Teams[NameOfTeamToGivePoint]:GetPlayers() do
player.leaderstats.Exp.Value += AmountOfPointsToGive
end
end
end