Hey scriptinghelpers, I have a leaderstats script that I want to add this function too. I did some testing and, it didn't work where I added it. Below you will find the leaderstats script and the function I want to add to it. The function is to add value to leaderstats when a player kills another player, I just don't know where it fits on my script is the problem. All the code is already written by me. It saves and adds 1 every second. Thank you for the help in advance, I appreciate it!
leaderstats script inside of ServerScriptStorage with a folder folder inside of the script named leaderstats
local players = game:GetService("Players")
local dataStore = game:GetService("DataStoreService")
local tixStore = dataStore:GetDataStore("TixStore")
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local tix = Instance.new("IntValue")
tix.Name = "Tix"
tix.Value = 0
tix.Parent = leaderstats
local data
local success, errorMessage = pcall(function()
data = tixStore:GetAsync(player.UserId.."-tix")
end)
if success then
tix.Value = data
else
warn("Error loading ".. player.Name .."'s data".. errorMessage)
end
end
players.PlayerAdded:Connect(leaderboardSetup)
players.PlayerRemoving:Connect(function(player)
local data = player.leaderstats.Tix.Value
local success, errorMessage = pcall(function()
tixStore:SetAsync(player.UserId.."-tix", data)
end)
if not success then
warn("Error saving ".. player.Name .."'s data".. errorMessage)
end
end)
while true do
wait(1)
local playerList = players:GetPlayers()
for currentPlayer = 1, #playerList do
local player = playerList[currentPlayer]
local tix = player.leaderstats.Tix
tix.Value = tix.Value + 1
end
end
The function I want to add to my leaderstats script to add value when a player kills another player.
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.Tix.Value = leaderstats.Tix.Value + 100

3 comments
Where do I put the LocalScript? I tried putting it in ServerScriptStorage but, it didn't do anything when I tested. What I did is I created a RemoteEvent, I put it in ReplicatedStorage. I added that chunk of script to the very bottom of my leaderstats script under the last end.
So, I've done some more testing. My computer might not be fast enough to test it. Like I said I added a RemoteEvent to ReplicatedStorage, I made a LocalScript with what you said, and I added the chunk of script to my leaderstats script (I tried 2 different places. One at the very bottom line of code Line 44 and the other one I tried was by making it Line 21 and lowering everything else.)
I'm not getting any output either so I'm not really sure.