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

Avatar Scaling / data store not working?

Asked by 6 years ago

Today I was testing my project and I was adding saving player scale. I did a testing server with a few friends and I noticed when they reset, died or left and rejoined, their size would increase dramatically. I’m not sure what is causing this issue, I’ve looked into it and can’t find anything that would increase the size with-out a player activating the tool or the script changing the scale by force.

If you can help then I would appreciate it! Also include what I'm doing wrong.

DataStore script:

local Data = game:GetService("DataStoreService"):GetDataStore("I")

game:GetService("Players").PlayerAdded:Connect(function(Plr)

    --  // Leaderstats

    local Stats = Instance.new("Folder", Plr)
    Stats.Name = "leaderstats"

    local Strength = Instance.new("IntValue", Stats)
    Strength.Name = "Strength"  

    local Rebirths = Instance.new("IntValue", Stats)
    Rebirths.Name = "Rebirths"

    -- // Alt values

    local Scales = Instance.new("Folder", Plr)
    Scales.Name = "Scales"

    local BWS = Instance.new("NumberValue", Scales)
    BWS.Name = "BWS"    

    local BHS = Instance.new("NumberValue", Scales)
    BHS.Name = "BHS"

    local BDS = Instance.new("NumberValue", Scales)
    BDS.Name = "BDS"

    -- // Loading stuff

    local Data1 = Data:GetAsync(Plr.UserId)

    if Data1 then
        for i, v in pairs(Stats:GetChildren()) do
            v.Value = Data1[v.Name]
        end
        for i, v in pairs(Scales:GetChildren()) do
            v.Value = Data1[v.Name]
        end
        print(Plr.Name .. "'s data loaded successfully!")
    end

    -- // Saving stuff

    spawn(function()
        while wait(60) do
            if Stats then
                local SaveData = {}
                for i, v in pairs(Stats:GetChildren()) do
                    SaveData[v.Name] = v.Value
                end

                for i, v in pairs(Scales:GetChildren()) do
                   SaveData[v.Name] = v.Value
                end

                Data:SetAsync(Plr.UserId, SaveData)
                print(Plr.Name .. "'s data autosaved successfully!")
            end
        end
    end)

    Plr.CharacterAdded:Connect(function(Char)
        wait(1)
        repeat wait() until Char:FindFirstChild("Humanoid")
        local Humanoid = Char:WaitForChild("Humanoid")
        local BDS = Humanoid:WaitForChild("BodyDepthScale")
        local BHS = Humanoid:WaitForChild("BodyHeightScale")
        local BWS = Humanoid:WaitForChild("BodyWidthScale")
        if Plr.leaderstats.Strength.Value == 0 then
            BWS.Value = 0.2
            BDS.Value = 0.2
            BHS.Value = 1
            Plr:FindFirstChild("Scales").BDS.Value = 0.2
            Plr:FindFirstChild("Scales").BHS.Value = 1
            Plr:FindFirstChild("Scales").BWS.Value = 0.2
        else
            BWS.Value = Plr:FindFirstChild("Scales").BWS.Value
            BHS.Value = Plr:FindFirstChild("Scales").BHS.Value
            BDS.Value = Plr:FindFirstChild("Scales").BDS.Value
        end
    end)
end)

game:GetService("Players").PlayerRemoving:Connect(function(Plr)
    local Stats = Plr:FindFirstChild("leaderstats")
    local WeightStats = Plr:FindFirstChild("Scales")
    if Stats then
        local SaveData = {}
        for i, v in pairs(Stats:GetChildren()) do
            SaveData[v.Name] = v.Value
        end
        for i, v in pairs(WeightStats:GetChildren()) do
            if v.ClassName == "NumberValue" then
                SaveData[v.Name] = v.Value
            end
        end
        Data:SetAsync(Plr.UserId, SaveData)
        print(Plr.Name .. "'s data autosaved successfully!")
    end
end)

Tool local script:

local Tool = script.Parent
local Player = game:GetService("Players").LocalPlayer
local Animation = Tool:WaitForChild("Handle").Animation
local Liftable = false
local UIS = game:GetService("UserInputService")
local LastActivated = tick()

function Check()
    if Liftable == true then
        UIS.InputBegan:Connect(function(Input, GPE)
            if GPE then
                return
            end

            if Input.KeyCode == Enum.KeyCode.E and Liftable == true then
                if tick() - LastActivated >= 2.5 then
                    print("Input Success")
                    local AnimPlay = Player.Character:FindFirstChild("Humanoid"):LoadAnimation(Animation)
                    AnimPlay:Play()
                    Tool.Fire:FireServer(Player, "IsUpdatedByGame")
                    LastActivated = tick()
                end
            end
        end)
    end

    if Liftable == false then
        UIS.InputEnded:Connect(function(Input, GPE)
            if GPE then
                return
            end

            if Input.KeyCode == Enum.KeyCode.E and Liftable == false then
                return 
            end
        end)
    end
end


Tool.Equipped:Connect(function()
    if Liftable == false then
        Liftable = true
        wait(0.1)
        Check()
        wait()
        print("Liftable set to true!")
    end
end)

Tool.Unequipped:Connect(function()
    if Liftable == true then
        Liftable = false
        wait(0.1)
        Check()
        wait()
        print("Liftable set to false!")
    end
end)

Tool Server script:

local Tool = script.Parent

Tool.Fire.OnServerEvent:Connect(function(Player, Check)
    wait(0.1)
    Player.leaderstats.Strength.Value = Player.leaderstats.Strength.Value + 1
    Player.Scales.BWS.Value = Player.Scales.BWS.Value + 0.2
    Player.Scales.BHS.Value = Player.Scales.BHS.Value + 0.1
    Player.Scales.BDS.Value = Player.Scales.BDS.Value + 0.2
    Player.Character:FindFirstChild("Humanoid").BodyWidthScale.Value = Player.Character:FindFirstChild("Humanoid").BodyWidthScale.Value + 0.03
    Player.Character:FindFirstChild("Humanoid").BodyHeightScale.Value = Player.Character:FindFirstChild("Humanoid").BodyHeightScale.Value + 0.02
    Player.Character:FindFirstChild("Humanoid").BodyDepthScale.Value = Player.Character:FindFirstChild("Humanoid").BodyDepthScale.Value + 0.03
end)
0
Your event UserInputService event is inside a function, which is not okay. User#19524 175 — 6y

Answer this question