Hey all, I am working on my swordfighting game and I have ran into an issue with the combat logging system. I need the Player’s time to reset when they leave in the fightzone.
game.Players.PlayerRemoving:Connect(function(Player)
if Player.InFightZone.Value == true then
warn(Player.Name.." combat logged! lol")
Player.leaderstats.Time.Value = 0
warn(Player.Name.." time reset due to combat logging!")
end
This works sometimes in studio, however, in game it never works. Any support will be greatly appreciated!
Is the datastore script in the same player leaved as the combat logged script? If not, then delete this script and move the combat log script to before the data saving script.
I have attempted this and I couldn’t get it to work. I believe it’s due to the placement of that function. Could you assist me on where to put the code?
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")
Players.PlayerAdded:Connect(function(player)
local Data = nil
local success, errormessage = pcall(function()
Data = Saver:GetAsync(tostring(player.UserId))
end)
if success then
if Data then
for i, v in pairs(Data) do
player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
end
end
else
error(errormessage)
end
end)
local function Save(Player)
local InFightZone = Player:WaitForChild("InFightZone")
if InFightZone.Value == true then
warn(Player.Name.." combat logged! lol")
Player.leaderstats.Time.Value = 0
warn(Player.Name.." time reset due to combat logging!")
else
local SavedData = {}
for _, v in pairs(Player.leaderstats:GetChildren()) do
SavedData[v.Name] = v.Value
end
local success, errormessage = pcall(function()
Saver:SetAsync(tostring(Player.UserId), SavedData)
end)
if not success then
error(errormessage)
end
end
end
Players.PlayerRemoving:Connect(Save)
game:BindToClose(function()
for _, v in pairs(Players:GetPlayers()) do
Save(v)
end
end)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")
Players.PlayerAdded:Connect(function(player)
local Data = nil
local success, errormessage = pcall(function()
Data = Saver:GetAsync(tostring(player.UserId))
end)
if success then
if Data then
for i, v in pairs(Data) do
player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
end
end
else
error(errormessage)
end
end)
local function Save(Player)
local InFightZone = Player:WaitForChild("InFightZone")
if InFightZone.Value == true then
warn(Player.Name.." combat logged! lol")
Player.leaderstats.Time.Value = 0
warn(Player.Name.." time reset due to combat logging!")
end
local SavedData = {}
for _, v in pairs(Player.leaderstats:GetChildren()) do
SavedData[v.Name] = v.Value
end
local success, errormessage = pcall(function()
Saver:SetAsync(tostring(Player.UserId), SavedData)
end)
if not success then
error(errormessage)
end
end
Players.PlayerRemoving:Connect(Save)
game:BindToClose(function()
for _, v in pairs(Players:GetPlayers()) do
Save(v)
end
end)