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

I'm having trouble with Instance.new. Why is this code not creating the Int Value?

Asked by 5 years ago

I'm trying to make a health bar while not using the player's actual health. I'm trying to use an Integer Value to replace the player's health. This is my code but it seems that the Instance.new isn't working.

wait(1)

--Variables
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid
local hpBar = script.Parent.Bar



--Functions
game.Players.PlayerAdded:Connect(function()
    local health = Instance.new("IntValue")
    health.Name = "Health"
    health.Parent = player
    health.Value = 100
end)

function Update()
    local hp = player.Health.Value / 100 - 0.02
    if hp > 2 then
        hpBar:TweenSize(UDim2.new(hp, 0, 0.85, 0))
    else
        humanoid.Health = 0
    end
end

Update()

player.Health.Changed:Connect(Update)
0
Is this a localscript or script? SoftlockedUnderZero 668 — 5y
0
because "LocalPlayer" from "Players" comes nil when server sided and "character" runs nil when client sided SoftlockedUnderZero 668 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

I have some possible conclusions:

  • Is this a local script? If it is you should put it in playerStarterScripts or something like that.

  • You shouldn't set the Instance.New() to local, it needs to be global

  • Check if the script even runs with debug print("test")

0
Your second conclusion makes zero sense. He is never using the health variable outside of the function it was defined in, so there is no reason why he should make it global. DeceptiveCaster 3761 — 5y
0
I tried the print("test") and it didn't work, I don't know what is wrong with the game.Players.PlayerAdded Legendary_Ark 0 — 5y
Ad

Answer this question