Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Where would I add this function to my leaderstats script?

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

1 answer

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)

3 comments

0

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.

0

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.)

0

I'm not getting any output either so I'm not really sure.

This discussion is preserved as it was. New answers, comments, and votes are closed.