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

Trying To Make ToolSave Not Dupe Tools AND Making Tool Check?

Asked by 4 years ago

The Title Is Confusing IK Buuuutt i will try to explain it

The First Script im making is a script to save tools and when you reset it duplicates the basic starter tool, so i want it to not dupe and i have tried an IF statement shown here:

char.Humanoid.Died:connect(function()
            char.Humanoid:UnequipTools()
            if player.Backpack:WaitForChild("Basic Halo") == true
            then
                print("Already Has Basic")
                else
        local old = player.StarterGear:GetChildren()
        for i = 1, #old do
            old[i]:Destroy()
        end

        local new = player.Backpack:GetChildren()
        for i = 1, #new do
            new[i].Parent = player.StarterGear
        end     
            end 
            end)    

Second Script: the second one is a local script to check if the player has the Molten Halo Tool when joining if the player does it will not give them the starter halo....

ToolSave Script (Normal Script, Server Script Service)

local ds = game:GetService("DataStoreService")
local store = ds:GetDataStore("saveStore")
local library = game:GetService("ServerStorage").ShopItems

--< directory functions

local dir = {}

local function edit(player, list)
    dir[player.Name] = list
end

local function setup(player, list)
    for i = 1, #list do
        local tool = library:FindFirstChild(list[i])
        if tool then
            local clone = tool:Clone()
            clone.Parent = player.Backpack
        else
            print(list[i] .. " not found")
        end
    end
end

--< player events

game.Players.PlayerAdded:connect(function(player)

local ready = false

    player.CharacterAdded:connect(function(char)

    local bp = player.Backpack
    local data = nil

    if ready == false then
        ready = true

        data = store:GetAsync(player.UserId)

        if data then
            setup(player, data)
            edit(player, data)
        end
    end 

    char.Humanoid.Died:connect(function()
            char.Humanoid:UnequipTools()
            if player.Backpack:WaitForChild("Basic Halo") == true
            then
                print("Already Has Basic")
                else
        local old = player.StarterGear:GetChildren()
        for i = 1, #old do
            old[i]:Destroy()
        end

        local new = player.Backpack:GetChildren()
        for i = 1, #new do
            new[i].Parent = player.StarterGear
        end     
            end 
            end)    


    --< adjuster

    local count = 0

    local function adjust()

    if char.Humanoid.Health > 0 then

        local list = {}

        local equipped = char:FindFirstChildOfClass("Tool")
        if equipped then
            table.insert(list, equipped.Name)
        end 

        local tools = bp:GetChildren()
        for i = 1, #tools do
            table.insert(list, tools[i].Name)
        end

        if count ~= #list then
            edit(player, list)
            count = #list
        end
    end
    end

    --< child events

    bp.ChildAdded:connect(adjust)   
    bp.ChildRemoved:connect(adjust) 

    char.ChildAdded:connect(function(child)
        if child.ClassName == "Tool" then
            adjust()
        end
    end)

    char.ChildRemoved:connect(function(child)
        if child.ClassName == "Tool" then
            adjust()
        end
    end)    

    end)
end)

game.Players.PlayerRemoving:connect(function(player)
    store:SetAsync(player.UserId, dir[player.Name])
    dir[player.Name] = nil
end)

--< safety

game:BindToClose(function()
    wait(5)
end)

--[[ Shiro75 ]]--

the local script to check if the player has the tool:

local player = game.Players.LocalPlayer

function checkBasic()
    wait(0.6)
    if player.Backpack:GetFirstChild("Molten Halo") == true
        then
        player.Backpack:GetFirstChild("Basic Halo"):Destroy()
        end 
    end

game.Players.ChildAdded:Connect(checkBasic)

Thanks Joe.

1 answer

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

This script was taken from a youtuber's video. You're most likely getting errors because you didn't modify the script and instead just copied and pasted it with a little name change.

Also to note, you should ALWAYS check server-side, never trust the client. Exploiters can easily spoof the client.

The first script is unecessary, that is what is most likely causing the duplication of tools. StarterGear is what tools the player will spawn with. When player respawns with a tool in their StarterGear they get their tool back, you also clone the same tool to their backpack thus making 2 copies of that tool.

local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("YourDataStoreHere")
local toolStorage = game:GetService("ServerStorage"):WaitForChild("ShopTools") --The folder might not be loaded it so we use WaitForChild just in case

local function onPlayerAdded(Player)
    local Data
    local suc,err = pcall(function() --Make a pcall function
        Data = DataStore:GetAsync(Player.UserId)
    end)
    if suc then
        print("Success!") --If the pcall was a success we let the server know
    else
        print("Error!") --If the pcall was erroring we let the server know
    end
    if Data then --If the player has tools saved then
        for _,v in pairs(Data) do
            for _,t in pairs(toolStorage:GetChildren()) do --Iterate through the folder
                if v == t.Name --If the tool name and the player data tools match then
                    t:Clone().Parent = Player.StarterGear
                    if not Player.Backpack:FindFirstChild("t.Name") then
                        t:Clone().Parent = Player.Backpack
                    end
                end
            end
        end
    else
        if not Player.Backpack:FindFirstChild("TheTool") then --Replace TheTool with your tool name
            toolStorage.TheTool:Clone().Parent = Player.Backpack
            toolStorage.TheTool:Clone().Parent = Player.StarterGear
        end
    end
end

local function onPlayerRemoved(Player)
    local DataToSave = {}
    for _,v in pairs(Player.StarterGear:GetChildren()) do
        table.insert(DataToSave, v.name)
    end
    local suc,err = pcall(function()
        DataStore:SetAsync(Player.UserId, DataToSave)
    end)
end)

game:GetService("Players").PlayerAdded:Connect(onPlayerAdded)
game:GetService("Players").PlayerRemoving:Connect(onPlayerRemoved)

game:BindToClose(function()
    for _,Player in pairs(game:GetService("Players"):GetPlayers()) do
        onPlayerRemoved(Player)
    end
end)

The script you provided had a lot of deprecated functions which I had to replace. I'm sorry if this didn't work. I rushly typed this so comment on this answer if you're receiving errors and I'll happily help you.

Ad

Answer this question