Hello, i’m using Alvin Blox’s Sword game Tutorial And I noticed that Whenever a player Won It Only Rewarded the money to a different player, the behaviour i seen is that If i win i get the money but if another person won i still Got the money, The code i have so far is
-- Variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local Status = ReplicatedStorage:WaitForChild("MainGameEvents"):WaitForChild("Main"):WaitForChild("Status")
local GameLength = 70
local reward = 100
--game loop
while true do
Status.Value = "Waiting for enough players"
repeat wait() until game.Players.NumPlayers >= 2
Status.Value = "Intermission"
wait(7)
local plrs = {}
for i, player in pairs(game.Players:GetPlayers()) do
if player then
table.insert(plrs,player) --- add teh players incase if a player joins mid game
end
end
wait(2)
local AvailaibleMaps = MapsFolder:GetChildren()
local ChosenMap = AvailaibleMaps[math.random(1,#AvailaibleMaps)]
Status.Value = ChosenMap.Name.." Chosen"
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace
--telport PLayers to map
local SpawnPoint = ClonedMap:FindFirstChild("Spawn")
if not SpawnPoint then
warn("Spawn point has not been found!")
end
local availableSpawnPoints = SpawnPoint:GetChildren()
for i, player in pairs(plrs) do
if player then
character = player.Character
if character then
--teleports them
character:FindFirstChild("HumanoidRootPart").CFrame = availableSpawnPoints[1].CFrame
table.remove(availableSpawnPoints,1)
--give sword
local sword = ServerStorage.Tools.Sword:Clone()
sword.Parent = player.Backpack
local gametag = Instance.new("BoolValue")
gametag.Name = "GameTag"
gametag.Parent = player.Character
else
--this is not a player so we will remove them from the table
if not player then
table.remove(plrs,i)
end
end
end
end
Status.Value = "Get ready to play!"
wait(2)
for i = GameLength,0,-1 do
for i, player in pairs(plrs) do
if player then
character = player.Character
if not character then
--probably left teh game cuz its too hard
else
if character:FindFirstChild("GameTag") then
print(player.Name.."is actually alive")
else
--they oofed
table.remove(plrs,x)
end
end
else
table.remove(plrs,x)
print(player.Name.."has been removed")
end
end
Status.Value = "There are "..i.." seconds remaining, and "..#plrs.." players left"
if #plrs == 1 then
--HE WON WOOOOOOOOOOOW
Status.Value = "The Winner is "..plrs[1].Name
plrs[1].leaderstats.Cash.Value = plrs[1].leaderstats.Cash.Value + reward --part where i think it errors
break
elseif #plrs == 0 then
Status.Value = "Nobody won"
elseif i ==0 then
Status.Value = "Times up!"
break
end
wait(1)
end
print("End of game")
for i, player in pairs(game.Players:GetPlayers()) do
character = player.Character
if not character then
--ignore
else
if character:FindFirstChild("GameTag") then
character.GameTag:Destroy()
end
if player.Backpack:FindFirstChild("Sword") then
player.Backpack.Sword:Destroy()
end
if character:FindFirstChild("Sword") then
character.Sword:Destroy()
end
end
player:LoadCharacter()
end
ClonedMap:Destroy()
Status.Value = "Game ended"
end
is that i did plr[1] so it looks at the first player in the leaderboard which is me?
Never mind, seems I read it wrong . I don’t see any problems here, so I’m not sure why it’s giving players who died the cash.
Edit: found it. You remove “x” which is never defined.
Use table.find(player) as the second argument
-- Variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local Status = ReplicatedStorage:WaitForChild("MainGameEvents"):WaitForChild("Main"):WaitForChild("Status")
local GameLength = 70
local reward = 100
--game loop
while true do
Status.Value = "Waiting for enough players"
repeat wait() until game.Players.NumPlayers >= 2
Status.Value = "Intermission"
wait(7)
local plrs = {}
for i, player in pairs(game.Players:GetPlayers()) do
if player then
table.insert(plrs,player) --- add teh players incase if a player joins mid game
end
end
wait(2)
local AvailaibleMaps = MapsFolder:GetChildren()
local ChosenMap = AvailaibleMaps[math.random(1,#AvailaibleMaps)]
Status.Value = ChosenMap.Name.." Chosen"
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace
--telport PLayers to map
local SpawnPoint = ClonedMap:FindFirstChild("Spawn")
if not SpawnPoint then
warn("Spawn point has not been found!")
end
local availableSpawnPoints = SpawnPoint:GetChildren()
for i, player in pairs(plrs) do
if player then
character = player.Character
if character then
--teleports them
character:FindFirstChild("HumanoidRootPart").CFrame = availableSpawnPoints[1].CFrame
table.remove(availableSpawnPoints,1)
--give sword
local sword = ServerStorage.Tools.Sword:Clone()
sword.Parent = player.Backpack
local gametag = Instance.new("BoolValue")
gametag.Name = "GameTag"
gametag.Parent = player.Character
else
--this is not a player so we will remove them from the table
if not player then
table.remove(plrs,i)
end
end
end
end
Status.Value = "Get ready to play!"
wait(2)
for i = GameLength,0,-1 do
for i, player in pairs(plrs) do
if player then
character = player.Character
if not character then
--probably left teh game cuz its too hard
else
if character:FindFirstChild("GameTag") then
print(player.Name.."is actually alive")
else
--they oofed
table.remove(plrs,table.find(plrs, player))
end
end
else
table.remove(plrs,table.find(plrs, player))
print(player.Name.."has been removed")
end
end
Status.Value = "There are "..i.." seconds remaining, and "..#plrs.." players left"
if #plrs == 1 then
--HE WON WOOOOOOOOOOOW
Status.Value = "The Winner is "..plrs[1].Name
plrs[1].leaderstats.Cash.Value = plrs[1].leaderstats.Cash.Value + reward --part where i think it errors
break
elseif #plrs == 0 then
Status.Value = "Nobody won"
elseif i ==0 then
Status.Value = "Times up!"
break
end
wait(1)
end
print("End of game")
for i, player in pairs(game.Players:GetPlayers()) do
character = player.Character
if not character then
--ignore
else
if character:FindFirstChild("GameTag") then
character.GameTag:Destroy()
end
if player.Backpack:FindFirstChild("Sword") then
player.Backpack.Sword:Destroy()
end
if character:FindFirstChild("Sword") then
character.Sword:Destroy()
end
end
player:LoadCharacter()
end
ClonedMap:Destroy()
Status.Value = "Game ended"
end
Oh its okay just it seems to my friends its unfair since i got the money. But is it the part
for i = GameLength,0,-1 do
for i, player in pairs(plrs) do
if player then
character = player.Character
if not character then
--probably left teh game cuz its too hard
else
if character:FindFirstChild("GameTag") then
print(player.Name.."is actually alive")
else
--they oofed
table.remove(plrs,x)
end
end
else
table.remove(plrs,x)
print(player.Name.."has been removed")
end
end