Sorry for bad title, but I don't really know how to explain it. I have a lobby / game system here, where whenever 1 player remains, they win. But I want to reward the player with +100 Cash on every win.
for _, player in pairs(game.Players:GetChildren()) do --//Player Table if player.Team.Name == 'Players' then .insert(playing, player.Name) table end end if #playing == 0 then --//# of Players Left. Status.Value = ' Everyone has died.' wait(3) break elseif #playing == 1 then Status.Value = (playing[1].. ' Has Won!') wait(3) for _, player in pairs(playing) do print(playing) local Winner = playing[1] print(Winner) local PlayerData = Winner.data.Cash.Value -- Line 121 print(PlayerData) PlayerData.Cash.Value += 100
here is my error;
? { [1] = "joystuuu" } joystuuu ServerScriptService.Rounds:121: attempt to index nil with 'Cash'
PS. Heres the complete script if it helps.
--// Created: 2022-10-09 12:14AM --// Last Edited: 2022-10-09 6:10PM --//Variables local DataStoreService = game:GetService('DataStoreService') local myDataStore = DataStoreService:GetDataStore("myDataStore") local RoundLength = 5 local IntermissionLength = 5 local InRound = game.ReplicatedStorage.InRound local Status = game.ReplicatedStorage.Status local LobbySpawn = workspace.Lobby_Spawn local MapSpawn1 = workspace.Map_Spawn_1 local MapSpawn2 = workspace.Map_Spawn_2 local MapSpawn3 = workspace.Map_Spawn_3 local MapSpawn4 = workspace.Map_Spawn_4 local MapSpawnPoint = 0 --//Teleport on Round Start. InRound.Changed:Connect(function() if InRound.Value == true then wait(1) for _, player in pairs(game.Players:GetChildren()) do --//SPAWNING local char = player.Character if MapSpawnPoint == 0 then char.HumanoidRootPart.CFrame = MapSpawn1.CFrame MapSpawnPoint = MapSpawnPoint + 1 print ('Now will spawn at: '.. MapSpawnPoint) elseif MapSpawnPoint == 1 then char.HumanoidRootPart.CFrame = MapSpawn2.CFrame MapSpawnPoint = MapSpawnPoint + 1 print ('Now will spawn at: '.. MapSpawnPoint) elseif MapSpawnPoint == 2 then char.HumanoidRootPart.CFrame = MapSpawn3.CFrame MapSpawnPoint = MapSpawnPoint + 1 print ('Now will spawn at: '.. MapSpawnPoint) elseif MapSpawnPoint == 3 then char.HumanoidRootPart.CFrame = MapSpawn4.CFrame MapSpawnPoint = MapSpawnPoint + 1 print ('Now will spawn at: '.. MapSpawnPoint) elseif MapSpawnPoint ==4 then char.HumanoidRootPart.CFrame = MapSpawn4.CFrame MapSpawnPoint = 0 print ('Now will spawn at: '.. MapSpawnPoint) end end for _, player in pairs(game.Players:GetChildren()) do --// TEAMS on game START local char = player.Character local Humanoid = char:WaitForChild('Humanoid') player.Team = game.Teams.Players Humanoid.Died:Connect(function() --//Death during game player.Team = game.Teams.Lobby end) end else wait(1) for _, player in pairs(game.Players:GetChildren()) do local char = player.Character player.Team = game.Teams.Lobby --//TEAMS on game END char.HumanoidRootPart.CFrame = LobbySpawn.CFrame end end end) local function roundTimer() while wait() do for i = IntermissionLength, 1, -1 do InRound.Value = false wait(1) Status.Value = ("Intermission: "..i.. " Seconds Left.") end for i = RoundLength, 1, -1 do InRound.Value = true wait(1) Status.Value = ("Game: ".. i.. " Seconds Left.") local playing = {} for _, player in pairs(game.Players:GetChildren()) do --//Player Table if player.Team.Name == 'Players' then table.insert(playing, player.Name) end end if #playing == 0 then --//# of Players Left. Status.Value = ' Everyone has died.' wait(3) break elseif #playing == 1 then Status.Value = (playing[1].. ' Has Won!') wait(3) for _, player in pairs(playing) do print(playing) local Winner = playing[1] print(Winner) local PlayerData = Winner.data.Cash.Value print(PlayerData) PlayerData.Cash.Value += 100 end break end end end end spawn (roundTimer)
You're receiving this error because you're using data.Cash.Value
on a string. You need to get the player object.
Change line 119
local Winner = game:GetService("Players"):FindFirstChild(playing[1])