Hey everyone! I wanted to ask a question involving leaderstats.
So recently I wanted to add Kills to a game i’m co-creating. I found a script for it, and I tested it in a test game, and it worked. So I put it into the game I’m working on, and it doesn’t work. First of all, on my screen, it shows that I have 0 wins and everyone else has - wins. It’s like that on everyone’s screen but THEY have the 0. This obviously means that it isn’t working, and I have a theory on why this doesn’t work.
So I have another leaderstats in the game, which is Wins. This is the only possible thing that may be interfering with the Kills. So, I have a possibly stupid question for you guys. I noticed that at the beginning of each script involving leaderstats, they create a folder inside the leaderstats. Is it possible, that there can only be one folder with all the leaderstats inside? If this is not the case, does anyone have any possible solutions for my problem here? If you need the script, let me know. Also, sorry for such a long post, but this is an essential part for the game and I don’t know what I would do without the kills.
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Value = 0
kills.Parent = leaderstats
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.Died:Connect(function(Died)
local creator = Character.Humanoid:FindFirstChild("creator")
local leaderstats = creator.Value:FindFirstChild("leaderstats")
if creator ~= nil and creator.Value ~= nil then
leaderstats.Kills.Value = leaderstats.Kills.Value + 1
end
end)
end)
end)
And here is the main script for my game which involves wins:
-- Define Variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local Status = ReplicatedStorage:WaitForChild("Status")
local GameLength = 180
local reward = 1
-- Game Loop
while true do
Status.Value = "1 more player required to start the game"
repeat wait(1) until game.Players.NumPlayers >= 2
Status.Value = "Intermission"
wait(15)
local plrs = {}
for i, player in pairs(game.Players:GetPlayers()) do
if player then
table.insert(plrs,player)
end
end
wait(2)
local AvailableMaps = MapsFolder:GetChildren()
local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]
Status.Value = ChosenMap.Name.." Chosen"
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace
wait(2)
-- Teleport players to the map
local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
if not SpawnPoints then
print("SpawnPoints not found!")
end
local AvailableSpawnPoints = SpawnPoints:GetChildren()
for i, player in pairs(plrs) do
if player then
character = player.Character
if character then
-- Teleport them
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,10,0)
table.remove(AvailableSpawnPoints,1)
-- Give them a gear
local Gear = ServerStorage.Gear:Clone()
Gear.Parent = player.Backpack
local GameTag = Instance.new("BoolValue")
GameTag.Name = "GameTag"
GameTag.Parent = player.Character
else
-- There is no character
if not player then
table.remove(plrs,i)
end
end
end
end
for i = GameLength,0,-1 do
for i, player in pairs(plrs) do
if player then
character = player.Character
if not character then
-- Left the game
table.remove(plrs,i)
else
if character:FindFirstChild("GameTag") then
-- They are still alive
print(player.Name.." is still in the game!")
else
-- They are dead
table.remove(plrs,i)
print(player.Name.." has been removed!")
end
end
else
table.remove(plrs,i)
print(player.Name.." has been removed!")
end
end
Status.Value = "There are "..i.." seconds remaining, and "..#plrs.." players left"
if #plrs == 1 then
-- Last person standing
Status.Value = "The winner is "..plrs[1].Name
plrs[1].leaderstats.Wins.Value = plrs[1].leaderstats.Wins.Value + reward
break
elseif #plrs == 0 then
Status = "Nobody won!"
break
elseif i == 0 then
Status.Value = "Times up!"
break
end
wait(1)
end
print("End of game")
wait(2)
for i, player in pairs(game.Players:GetPlayers()) do
character = player.Character
if not character then
-- Ignore them
else
if character:FindFirstChild("GameTag") then
character.GameTag:Destroy()
end
if player.Backpack:FindFirstChild("Gear") then
player.Backpack.Gear:Destroy()
end
if character:FindFirstChild("Gear") then
character.Gear:Destroy()
end
end
player:LoadCharacter()
end
ClonedMap:Destroy()
Status.Value = "Game has ended"
wait(2)
end
I reccomend using this for the creating of leaderstats and things like that, then put your scripts following along:
function added(player)
local ls = Instance.new("IntValue")
ls.Name = "leaderstats"
local role = Instance.new("IntValue")
role.Name = "Kills"
local role2 = Instance.new("IntValue")
role2.Name = "Wins"
end
game.Players.PlayerAdded:connect(added)
Do I put this at the beginning of a script, or is this a completely new script? Also, how would I call this role to be used when I’m in the kill script?
function added(player)
local ls = Instance.new("IntValue")
ls.Name = "leaderstats"
local role = Instance.new("IntValue")
role.Name = "Kills"
local role2 = Instance.new("IntValue")
role2.Name = "Wins"
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.Died:Connect(function(Died)
local creator = Character.Humanoid:FindFirstChild("creator")
local leaderstats = creator.Value:FindFirstChild("leaderstats")
if creator ~= nil and creator.Value ~= nil then
leaderstats.Kills.Value = leaderstats.Kills.Value + 1
end
end)
end)
-- Define Variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local Status = ReplicatedStorage:WaitForChild("Status")
local GameLength = 180
local reward = 1
-- Game Loop
while true do
Status.Value = "1 more player required to start the game"
repeat wait(1) until game.Players.NumPlayers >= 2
Status.Value = "Intermission"
wait(15)
local plrs = {}
for i, player in pairs(game.Players:GetPlayers()) do
if player then
table.insert(plrs,player)
end
end
wait(2)
local AvailableMaps = MapsFolder:GetChildren()
local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]
Status.Value = ChosenMap.Name.." Chosen"
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace
wait(2)
-- Teleport players to the map
local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
if not SpawnPoints then
print("SpawnPoints not found!")
end
local AvailableSpawnPoints = SpawnPoints:GetChildren()
for i, player in pairs(plrs) do
if player then
character = player.Character
if character then
-- Teleport them
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,10,0)
table.remove(AvailableSpawnPoints,1)
-- Give them a gear
local Gear = ServerStorage.Gear:Clone()
Gear.Parent = player.Backpack
local GameTag = Instance.new("BoolValue")
GameTag.Name = "GameTag"
GameTag.Parent = player.Character
else
-- There is no character
if not player then
table.remove(plrs,i)
end
end
end
end
for i = GameLength,0,-1 do
for i, player in pairs(plrs) do
if player then
character = player.Character
if not character then
-- Left the game
table.remove(plrs,i)
else
if character:FindFirstChild("GameTag") then
-- They are still alive
print(player.Name.." is still in the game!")
else
-- They are dead
table.remove(plrs,i)
print(player.Name.." has been removed!")
end
end
else
table.remove(plrs,i)
print(player.Name.." has been removed!")
end
end
Status.Value = "There are "..i.." seconds remaining, and "..#plrs.." players left"
if #plrs == 1 then
-- Last person standing
Status.Value = "The winner is "..plrs[1].Name
plrs[1].leaderstats.Wins.Value = plrs[1].leaderstats.Wins.Value + reward
break
elseif #plrs == 0 then
Status = "Nobody won!"
break
elseif i == 0 then
Status.Value = "Times up!"
break
end
wait(1)
end
print("End of game")
wait(2)
for i, player in pairs(game.Players:GetPlayers()) do
character = player.Character
if not character then
-- Ignore them
else
if character:FindFirstChild("GameTag") then
character.GameTag:Destroy()
end
if player.Backpack:FindFirstChild("Gear") then
player.Backpack.Gear:Destroy()
end
if character:FindFirstChild("Gear") then
character.Gear:Destroy()
end
end
player:LoadCharacter()
end
ClonedMap:Destroy()
Status.Value = "Game has ended"
wait(2)
end
end
game.Players.PlayerAdded:connect(added)
No, it did not. Now, it chooses two maps to teleport two, and it teleports us to the first map, and then the second. Also, the kills have been completely wiped from the leaderstats.
I’m not sure if he meant to make leaderstats an intValue, but try changing the Instance.new to a Folder.
I think you should go back to your previous script, as the one he does above is practically the same thing. However instead of putting the wins and the main script in a separate one from the leader stats and kills, put it all on the same script inside of ServerScriptService.
The reason why it chooses two maps is most likely because you are playing with two people, and at the end of the script, he calls the function added. Basically he’s saying that every time a player joins, call that function. Well the problem is, inside that function, you also have your map choosing system, so every time a new player joins, that function chooses a new map. Like I said, just go back to your previous script and we can start over.
Never mind actually, try @kylerzong and his script. I think that’s what you need. You never created a win value in the first place.
Sorry that response was really bad. Revert back to your old two scripts, and add on a Win stat just like you did for Kills. Parent that to leaderstats as well. Also, I recommend putting the script that is currently in the workspace in ServerScriptService, as it won’t be able to be accessed by anyone in there.
I recommend placing your server-scripts inside the ServerScriptService, unless they have a special need in workspace, such as tools. Personally I would call this script Main and add what I need in the game there.
And yes, you can store your leaderstats values inside a Configuration, those are used to store String/Int/Bool/NumberValues and such.