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

Max Health increase in studio works perfectly but...?

Asked by 5 years ago
Edited 5 years ago

Whenever I increase max health in studio when a stat is changed, the health regens to the max health. However, in the actual game, the health only regens up to 100 regardless of how much the max health is. Here is the script:

player = game.Players.LocalPlayer
local Str = player.Stats.Strength.Value
local Agi = player.Stats.Agility.Value
local Int = player.Stats.Intelligence.Value
local Def = player.Stats.Defense.Value
local Vit = player.Stats.Vitality.Value
local Dex = player.Stats.Dexterity.Value
local Damage = 0
local Speed = 0
local Critical = 0
local CriticalDamage = 0
local weapon

function damage()
    for j,k in pairs(player.Equip:GetChildren()) do
        if k.itemType.Value == "Weapon" then
            Damage = k.itemDamage.Value
            Speed = k.itemSpeed.Value
            Critical = k.itemCritical.Value
            CriticalDamage = k.itemCritDamage.Value
            weapon = k
            script.Parent.Parent.Information.ATKDMG.Text = "ATKDMG: " .. tostring(Damage)
            script.Parent.Parent.Information.CRITCHANCE.Text = "Critical %: " .. tostring(Critical) .. "%"
            script.Parent.Parent.Information.CRITDMG.Text = "Critical DMG: " .. tostring(CriticalDamage)
        end
    end
end

function removeDamage()
    if weapon == nil then
        Damage = 0
        Speed = 0
        Critical = 0
        CriticalDamage = 0
        script.Parent.Parent.Information.ATKDMG.Text = "ATKDMG: " .. tostring(Damage)
    end
end

function onstatChange(x)
    wait(0.03)
        --Health--
    if x == "Strength" then
        player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth+2
        player.Character.Humanoid.Health = player.Character.Humanoid.Health + 4
        script.Parent.Parent.Information.HP.Text = "HP: " .. tostring(player.Character.Humanoid.MaxHealth)
        script.Parent.Parent.Information.ATKDMG.Text = "ATKDMG: " .. tostring(Damage)
    end
    print("Lmao")
    if x == "Agility" then
        script.Parent.Parent.Information.ATKSpeed.Text = "ATKSpeed: " .. tostring(Speed)
    end
    if x == "Intelligence" then
        script.Parent.Parent.Information.MP.Text = "MP: " .. tostring((player.Stats.Intelligence.Value*4)+100)
    end
    if x == "Defense" then
        script.Parent.Parent.Information.damageResistance.Text = "DMG Resist%: " .. tostring(player.Stats.Defense.Value/5) .. "%"
    end
    if x == "Vitality" then
        player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth+4
        player.Character.Humanoid.Health = player.Character.Humanoid.Health + 4
        wait(0.1)
        script.Parent.Parent.Information.HP.Text = "HP: " .. tostring(player.Character.Humanoid.MaxHealth)
        script.Parent.Parent.Information.Stamina.Text = "Stamina: " .. tostring(player.PlayerGui.mainUI.statHolder.staminaHolder.staminaBar.MaxStamina.Value)
    end
    if x == "Dexterity" then
        script.Parent.Parent.Information.CRITCHANCE.Text = "Critical %: " .. tostring(Critical) .. "%"
        script.Parent.Parent.Information.CRITDMG.Text = "Critical DMG: " .. tostring(CriticalDamage)
    end
end

function addChar()
    wait(0.1)
    player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth+(2*Str)+(4*Vit)
    player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth
    script.Parent.Parent.Information.HP.Text = "HP: " .. tostring(player.Character.Humanoid.MaxHealth)
    script.Parent.Parent.Information.MP.Text = "MP: " .. tostring((player.Stats.Intelligence.Value*4)+100)
    script.Parent.Parent.Information.ATKSpeed.Text = "ATKSpeed: " .. tostring(Speed)
    script.Parent.Parent.Information.ATKDMG.Text = "ATKDMG: " .. tostring(Damage)
    script.Parent.Parent.Information.Stamina.Text = "Stamina: " .. tostring(player.PlayerGui.mainUI.statHolder.staminaHolder.staminaBar.MaxStamina.Value)
    script.Parent.Parent.Information.CRITCHANCE.Text = "Critical %: " .. tostring(Critical) .. "%"
    script.Parent.Parent.Information.CRITDMG.Text = "Critical DMG: " .. tostring(CriticalDamage)
end

player.CharacterAdded:Connect(addChar)
player.Equip.ChildAdded:Connect(damage)
player.Equip.ChildRemoved:Connect(removeDamage)
player.Stats.Strength.Changed:Connect(function()
    onstatChange("Strength")
end)
player.Stats.Agility.Changed:Connect(function()
    onstatChange("Agility")
end)
player.Stats.Intelligence.Changed:Connect(function()
    onstatChange("Intelligence")
end)
player.Stats.Defense.Changed:Connect(function()
    onstatChange("Defense")
end)
player.Stats.Vitality.Changed:Connect(function()
    onstatChange("Vitality")
end)
player.Stats.Dexterity.Changed:Connect(function()
    onstatChange("Dexterity")
end)

So I tried this in my local script

if x == "Strength" then
        game.ReplicatedStorage.AddHealth:FireServer()
        script.Parent.Parent.Information.HP.Text = "HP: " .. tostring(player.Character.Humanoid.MaxHealth)
        script.Parent.Parent.Information.ATKDMG.Text = "ATKDMG: " .. tostring(Damage)
    end

and in my server script in workspace

game.ReplicatedStorage.AddHealth.OnServerEvent:connect(function(player)
    player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth+2
end)

it says AddHealth is not a valid member

2 answers

Log in to vote
0
Answered by 5 years ago

If you're using FilteringEnabled, you'll need to do that in a Script instead of a LocalScript.

1
How would I find the local player from a server side script? xxXTimeXxx 101 — 5y
1
You can send it over using a RemoteEvent. AvatarAang400 35 — 5y
0
Can you give me an example? I kept reading about using remoteevents as a means to get the player from a server-side script but I never seemed to find any non-complicated guide xxXTimeXxx 101 — 5y
2
Put a remote event into your Replicated Storage. In your local script, call it like: game.ReplicatedStorage.RemoteEvent:FireServer(). In your server, have a function that is triggered by game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(). Then, do whatever you need to in that function and you will have the player variable. AvatarAang400 35 — 5y
View all comments (4 more)
1
Okay thanks i'm gonna try that as soon as i finish eating lunch xxXTimeXxx 101 — 5y
0
Edited the code xxXTimeXxx 101 — 5y
0
Nvm lmao you needed to create the AddHealth remoteevent in the storage xD thanks xxXTimeXxx 101 — 5y
0
No problem! AvatarAang400 35 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
--You cant use

game.Players.LocalPlayer

--In a script you can only use it in a local script

Answer this question