Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this not add value to my leaderstats when a player kills another player?

Asked by
ElBamino 153
3 years ago

Hey scriptinghelpers, my question is why is my scripts not working the way I want them too? My example is if Builderman kills Telamon then add 100 tix to Builderman's leaderstats (Tix in my case). Below you will find all the code I am using. I have a RemoveEvent in ReplicatedStorage.

I'm using 2 different scripts for this. Like I said I have a RemoteEvent in ReplicatedStorage.

My leaderstats script in ServerScriptStorage. It's a "Script" with a folder inside of it 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
local RE = game.ReplicatedStorage.RemoteEvent
RE.OnServerEvent:Connect(function(player)  
    player.leaderstats.Tix.Value = player.leaderstats.Tix.Value + 100
end)

I also have a LocalScript in ServerScriptStorage.

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)

The testing I did was on ROBLOX studio using the "Test" with 2 players. I did not get any output and it didn't add any value to the player who killed the other player. (Like I said Builderman kills Telamon, give Builderman 100 Tix.)

Thanks for the help in advance, I just don't understand. I appreciate it!

Answer this question