so on this script i am trying to get the game to set a intvalue to the players health which doesnt work but it does set the players health to the value of the int value

so i just need to find a way to get it to set the int value to the players health value

script:

local DS2 = require(1936396537)
local runservice = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(plr)
    local speedData = DS2('Speed', plr)
    local healthData = DS2('Health', plr)
	local currencyData = DS2('Currency', plr)
	local experienceData = DS2('Experience', plr)
    
    local Speed = Instance.new("Folder")
    Speed.Name = "Speed"
    Speed.Parent = plr
    local Five = Instance.new("BoolValue")
    Five.Name = "Five"
    Five.Parent = Speed
    local Ten = Instance.new("BoolValue")
    Ten.Name = "Ten"
    Ten.Parent = Speed
    local Fifteen = Instance.new("BoolValue")
    Fifteen.Name = "Fifteen"
    Fifteen.Parent = Speed
    local Twenty = Instance.new("BoolValue")
    Twenty.Name = "Twenty"
    Twenty.Parent = Speed
    for i,v in pairs (Speed:GetChildren()) do
        v.Changed:connect(function()
            local Saving = {
                ["Five"] = (plr.Speed).Five.Value;
                ["Ten"] = (plr.Speed).Ten.Value;
                ["Fifteen"] = (plr.Speed).Fifteen.Value;
                ["Twenty"] = (plr.Speed).Twenty.Value;
            }
            speedData:Set(Saving)--Save in speed data with the saving table
            print('Saved '..plr.Name..'s Speed Data')
        end)
    end
    print("Gave "..plr.Name.." Speed Data")
    local SavedItems = speedData:Get(nil)--Get with a default value of nil.
    if SavedItems then
        Five.Value = SavedItems.Five or false
        Ten.Value = SavedItems.Ten or false
        Fifteen.Value = SavedItems.Fifteen or false
        Twenty.Value = SavedItems.Twenty or false
        print(plr.Name.." had Speed data")
    else
        Five.Value = false
        Ten.Value = false
        Fifteen.Value = false
        Twenty.Value = false
        print(plr.Name.." didn't have Speed data")
    end
    
    local MaxHealth = Instance.new("Folder")
    MaxHealth.Name = "MaxHealth"
    MaxHealth.Parent = plr
    local First = Instance.new("BoolValue")
    First.Name = "First"
    First.Parent = MaxHealth
    local Second = Instance.new("BoolValue")
    Second.Name = "Second"
    Second.Parent = MaxHealth
    local Third = Instance.new("BoolValue")
    Third.Name = "Third"
    Third.Parent = MaxHealth
    local Fourth = Instance.new("BoolValue")
    Fourth.Name = "Fourth"
    Fourth.Parent = MaxHealth
    local Fifth = Instance.new("BoolValue")
    Fifth.Name = "Fifth"
    Fifth.Parent = MaxHealth
    for i,v in pairs (MaxHealth:GetChildren()) do
        v.Changed:connect(function()
            local Saving = {
                ["First"] = (plr.MaxHealth).First.Value;
                ["Second"] = (plr.MaxHealth).Second.Value;
                ["Third"] = (plr.MaxHealth).Third.Value;
                ["Fourth"] = (plr.MaxHealth).Fourth.Value;
                ["Fifth"] = (plr.MaxHealth).Fifth.Value;
            }
            healthData:Set(Saving)--Save in health data with the saving table
           print('Saved '..plr.Name..'s MaxHealth Data')
        end)
    end
    print("Gave "..plr.Name.." MaxHealth Data")
    SavedItems = healthData:Get(nil)--Get with a default value of nil.
    if SavedItems then
        First.Value = SavedItems.First or false
        Second.Value = SavedItems.Second or false
        Third.Value = SavedItems.Third or false
        Fourth.Value = SavedItems.Fourth or false
        Fifth.Value = SavedItems.Fifth or false
        print(plr.Name.." had MaxHealth data")
    else
        First.Value = false
        Second.Value = false
        Third.Value = false
        Fourth.Value = false
        Fifth.Value = false
        print(plr.Name.." didn't have MaxHealth data")
    end

local Nut = Instance.new("Folder")
    Nut.Name = "Nut"
    Nut.Parent = plr
    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Parent = Nut
    local Moral = Instance.new("IntValue")
    Moral.Name = "Moral"
    Moral.Parent = Nut
   
    for i,v in pairs (Nut:GetChildren()) do
        v.Changed:connect(function()
            local Saving = {
                ["Coins"] = (plr.Nut).Coins.Value;
                ["Moral"] = (plr.Nut).Moral.Value;
            }
            currencyData:Set(Saving)
            print('Saved '..plr.Name..'s Currency Data')
        end)
    end
    print("Gave "..plr.Name.." Currency Data")
    local SavedItems = currencyData:Get(nil)
    if SavedItems then
        Coins.Value = SavedItems.Coins or 0
        Moral.Value = SavedItems.Moral or 0
        print(plr.Name.." had Currency data")
    else
        Coins.Value = 0
        Moral.Value = 0
        print(plr.Name.." didn't have Currency data")
    end

local Experience = Instance.new("Folder")
	Experience.Name = "Experience"
	Experience.Parent = plr
	local Will = Instance.new("IntValue")
	Will.Name = "Will"
	Will.Parent = Experience
	local Skill = Instance.new("IntValue")
	Skill.Name = "Skill"
	Skill.Parent = Experience
	local Strength = Instance.new("IntValue")
	Strength.Name = "Strength"
	Strength.Parent = Experience
	
	 for i,v in pairs (Experience:GetChildren()) do
        v.Changed:connect(function()
            local Saving = {
                ["Will"] = (plr.Experience).Will.Value;
                ["Skill"] = (plr.Experience).Skill.Value;
				["Strength"] = (plr.Experience).Strength.Value;
            }
            experienceData:Set(Saving)
            print('Saved '..plr.Name..'s Experience Data')
        end)

	   end
	print("Gave "..plr.Name.." Experience Data")
    local SavedItems = experienceData:Get(nil)
    if SavedItems then
        Will.Value = SavedItems.Will or 0
        Skill.Value = SavedItems.Skill or 0
		Strength.Value = SavedItems.Strength or 0
        print(plr.Name.." had Experience data")
    else
        Will.Value = 0
        Skill.Value = 0
		Strength.Value = 0
        print(plr.Name.." didn't have Experience data")
    end
	
local PlayerHealth = Instance.new("Folder")
	PlayerHealth.Name = "PlayerHealth"
	PlayerHealth.Parent = plr
	local Health = Instance.new("IntValue")
	Health.Name = "Health"
	Health.Parent = PlayerHealth
	
	 for i,v in pairs (PlayerHealth:GetChildren()) do
        v.Changed:connect(function()
            local Saving = {
                ["Health"] = (plr.PlayerHealth).Health.Value;
            }
            experienceData:Set(Saving)
            print('Saved '..plr.Name..'s PlayerHealth Data')
        end)

	   end
	print("Gave "..plr.Name.." PlayerHealth Data")
    local SavedItems = experienceData:Get(nil)
    if SavedItems then
        Health.Value = SavedItems.Health or 100
        print(plr.Name.." had PlayerHealth Data")
    else
        Health.Value = 100
        print(plr.Name.." didn't have PlayerHealth Data")
    end
	local Character = plr.Character or plr.CharacterAdded:Wait()
	Character.Humanoid.Health = Health.Value
    Character.Humanoid.HealthChanged:Connect(function(H)
	Health.Value = H
	end)
	
end)

game:BindToClose(function()
    if runservice:IsStudio() then
        return
    else
        wait(3)
    end
end)

5 comments

0

You should never update a datastore with a .Changed event. If this value frequently changes, you will be throttled and the updates will not take place. Maybe a once every 60 seconds would make more sense.

0

alright thank you

0

so how would i got about this then? use a while true do loop in the part that updates the value?

0

add player removing

0

greatneil this is datastore2 it saves the second a value changes so no need for player removing