Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

2 comments

0

Is this a localscript or script?

0

because "LocalPlayer" from "Players" comes nil when server sided and "character" runs nil when client sided

1 answer

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")

2 comments

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.

0

I tried the print("test") and it didn't work, I don't know what is wrong with the game.Players.PlayerAdded

This discussion is preserved as it was. New answers, comments, and votes are closed.