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
You cannot add that function to your leaderstats script.
You are calling a LocalPlayer
and therefore that function there needs to be called in a LocalScript
Only. No matter where you put that function it will not work.
You need to put it in a LocalScript
, then use a RemoteEvent
to change the players leaderstats.
To do this, have a RemoteEvent
called anything you like ready in your ReplicatedStorage
. In this case i'll just call it "RemoteEvent"
in your local script:
local RE = game.ReplicatedStorage.RemoteEvent local player =game.Players.LocalPlayer 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 RE:FireServer(player) end end) end)
in your main leaderstatsScript: insert this chunk of script to the very bottom END of your leaderstat script
local RE = game.ReplicatedStorage.RemoteEvent RE.OnServerEvent:Connect(function(player) player.leaderstats.Tix.Value = player.leaderstats.Tix.Value + 100 end)