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

How do I make a script that puts a tool in your inventory based on a leaderstat?

Asked by 4 years ago

Hello,

I have a leaderstat for kills, and I was wondering how I can have it give you a sword if you have a certain amount of kills. How could I do this?

Thank you, KreativleV2

0
you can accomplish this with using the YourStat.Changed() if YourStat.Value == AmountYouWant then code here end end) AntoninFearless 622 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

This is not that hard to make and achieve

First to make this easier you should make a reward dictionaries for example:

local rewards = {
    [100] = "Your sword name here",
    [500] = "Your sword name here"

    -- you can add as many as you want
}

After that you want to reward the player with :GetPropertyChangedSignal. If you use DataStore this script will also work because upon loading data your value will get change if I am not wrong

local SS = game:GetService("ServerStorage")
local rewards = {
    [100] = "Part",
    [500] = "Your sword name here"

    -- you can add as many as you want
}

local function giveTool(player)
    for requirement, reward in pairs(rewards) do
        if tonumber(requirement) <= kills.Value then
            local hasSword
            hasSword = player.Backpack:FindFirstChild(reward)

            if hasSword then
                return
            else
                local newSword = SS:WaitForChild(reward):Clone()
                newSword.Parent = player.Backpack
            end
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    -- your simple leaderstats script here
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local kills = Instance.new("IntValue")
    kills.Name = "kills"
    kills.Parent = leaderstats

    kills:GetPropertyChangedSignal("Value"):Connect(function()
        giveTool(player)
    end)

    player.Character.CharacterAdded:Connect(function()
        giveTool(player)
    end)
end)

Note: You might need to check if player is holding a sword to prevent multiple rewards Edit: As elyz said below, I edited it so it gives you the sword after dying. Just look at my answer as a sequel for multiple rewards.

Ad
Log in to vote
0
Answered by
Elyzzia 1294 Moderation Voter
4 years ago

like AntoninFearless said, you can use the .Changed event to detect when a player reaches a certain amount of kills and give them the sword

keep in mind that the player's backpack is cleared when they die, so you'll need to give them the sword when they respawn too


local swordKillRequirement = someNumberHere -- you should probably use a table for this if you're going to add lots of different swords, but for just one sword a variable is enough local sword = path.to.Tool Players.PlayerAdded:Connect(function(player) -- blablabla leaderstats stuff goes here leaderstats.Kills.Changed:Connect(function(kills) -- for Values, the .Changed event only fires when the value changes, unlike other types of instances where it fires for every property change local tool = player.Backpack:FindFirstChild(sword.Name) or player.Character:FindFirstChild(sword.Name) -- tools get parented to the character when they're equipped, so we need to check in the character too if kills >= swordKillRequirement and not tool then sword:Clone().Parent = player.Backpack end end) -- add the sword when the player respawns player.CharacterAdded:Connect(function() if leaderstats.Kills.Value >= swordKillRequirement then sword:Clone().Parent = player.Backpack end end) -- datastore stuff goes here, if you have it end)
0
Where should I store all of the swords? KreativleV2 0 — 4y

Answer this question