generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Script only works once

Previous Thread :: Next Thread 
Avocation is not online. Avocation
Joined: 22 Mar 2011
Total Posts: 107
20 Jun 2014 10:56 AM
I need help this script only works once when your health changes how do I fix it?

local char = script.Parent.Parent.Parent.Parent.Parent.Character
hum = char.Humanoid
hum.Changed:connect(function()
script.Parent.Health.Frame.Size = UDim2.new(1, 0, hum.Health/hum.MaxHealth, 0)
if hum.Health <= hum.MaxHealth*0.15 then
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(150, 0, 0)
else
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(0, 185, 0)
end
end)
Report Abuse
WolfzBoii is not online. WolfzBoii
Joined: 29 Jul 2013
Total Posts: 135
20 Jun 2014 10:57 AM
Maybe add while true do at the beginning?
Report Abuse
moryo7 is not online. moryo7
Joined: 19 Jul 2010
Total Posts: 5028
20 Jun 2014 10:59 AM
local char = script.Parent.Parent.Parent.Parent.Parent.Character
hum = char.Humanoid
locked = false
hum.Changed:connect(function()
if locked == false then locked = true
script.Parent.Health.Frame.Size = UDim2.new(1, 0, hum.Health/hum.MaxHealth, 0)
if hum.Health <= hum.MaxHealth*0.15 then
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(150, 0, 0)
else
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(0, 185, 0)
end
end
end)
Report Abuse
moryo7 is not online. moryo7
Joined: 19 Jul 2010
Total Posts: 5028
20 Jun 2014 10:59 AM
OH, you wanted it not to only work once, my bad. I'll make a new one. :P
Report Abuse
TheProM is not online. TheProM
Joined: 27 Mar 2013
Total Posts: 436
20 Jun 2014 11:04 AM
while true do
local char = script.Parent.Parent.Parent.Parent.Parent.Character
hum = char.Humanoid
hum.Changed:connect(function()
script.Parent.Health.Frame.Size = UDim2.new(1, 0, hum.Health/hum.MaxHealth, 0)
if hum.Health <= hum.MaxHealth*0.15 then
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(150, 0, 0)
else
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(0, 185, 0)
end
end)
end
Report Abuse
zsoltisawesome is not online. zsoltisawesome
Joined: 09 Sep 2010
Total Posts: 193
20 Jun 2014 11:05 AM
Does this work?

local char = script.Parent.Parent.Parent.Parent.Parent.Character
hum = char.Humanoid
while true do
hum.Changed:connect(function()
script.Parent.Health.Frame.Size = UDim2.new(1, 0, hum.Health/hum.MaxHealth, 0)
if hum.Health <= hum.MaxHealth*0.15 then
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(150, 0, 0)
else
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(0, 185, 0)
end
end)
end
Report Abuse
zsoltisawesome is not online. zsoltisawesome
Joined: 09 Sep 2010
Total Posts: 193
20 Jun 2014 11:07 AM
@TheProM

No need to redefine variables..
Report Abuse
TheProM is not online. TheProM
Joined: 27 Mar 2013
Total Posts: 436
20 Jun 2014 11:08 AM
Ops. I forgot that. :P
Report Abuse
smiley599 is not online. smiley599
Joined: 23 Jan 2010
Total Posts: 21869
20 Jun 2014 11:17 AM
Why would you put while true do -_-
Report Abuse
Avocation is not online. Avocation
Joined: 22 Mar 2011
Total Posts: 107
20 Jun 2014 11:56 AM
I tried zsoltisawesome's but roblox isn't loading with it I'll try with the while true do now xD
Report Abuse
zsoltisawesome is not online. zsoltisawesome
Joined: 09 Sep 2010
Total Posts: 193
20 Jun 2014 11:57 AM
ERM! big screwup on my part, forgot to tell it to sleep the loop, ROBLOX crashes otherwise:

local char = script.Parent.Parent.Parent.Parent.Parent.Character
hum = char.Humanoid
while true do
wait(0.1)
hum.Changed:connect(function()
script.Parent.Health.Frame.Size = UDim2.new(1, 0, hum.Health/hum.MaxHealth, 0)
if hum.Health <= hum.MaxHealth*0.15 then
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(150, 0, 0)
else
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(0, 185, 0)
end
end)
end

^ that should work
Report Abuse
Avocation is not online. Avocation
Joined: 22 Mar 2011
Total Posts: 107
20 Jun 2014 12:00 PM
Thanks xD
Report Abuse
smiley599 is not online. smiley599
Joined: 23 Jan 2010
Total Posts: 21869
20 Jun 2014 12:29 PM
Why is there a while loop -_-
Report Abuse
Avocation is not online. Avocation
Joined: 22 Mar 2011
Total Posts: 107
20 Jun 2014 12:40 PM
While true do is an infinite loop so I guess it makes it repeat the search for a health change?
Report Abuse
Locard is not online. Locard
Joined: 13 Apr 2014
Total Posts: 3516
20 Jun 2014 12:45 PM
All of you that have just posted a while loop do not know how to code efficiently. I'm sorry, but it's true.
Here, try this:

local player = script.Parent.Parent.Parent.Parent.Parent
player.Character.Humanoid.Changed:connect(function(prop)
if prop == "Health" then
script.Parent.Health.Frame.Size = UDim2.new(1,0,player.Character.Humanoid.Health/player.Character.Humanoid.MaxHealth,0)
if player.Character.Humanoid.Health <= player.Character.Humanoid.MaxHealth*.15 then
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(150/255,0,0)
else
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(0,185/255,0)
end
end
end)

The one you had before runs every time Humanoid has changed, which us quite literally all the time. The one I gave you makes it run only when it's the property 'Health' that changed.
Report Abuse
smiley599 is not online. smiley599
Joined: 23 Jan 2010
Total Posts: 21869
20 Jun 2014 12:50 PM
All of you that have just posted a while loop do not know how to code efficiently. I'm sorry, but it's true. [2]
Report Abuse
Meowcatmaster is not online. Meowcatmaster
Joined: 20 Oct 2012
Total Posts: 769
20 Jun 2014 12:51 PM
LP = Players.LocalPlayer
repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:findFirstChild("Humanoid")
while true do
LP.Character.Humanoid.MaxHealth = math.huge
wait(5)
end
--Was it just a health change script?
Report Abuse
smiley599 is not online. smiley599
Joined: 23 Jan 2010
Total Posts: 21869
20 Jun 2014 01:01 PM
No just no ^^
Report Abuse
Meowcatmaster is not online. Meowcatmaster
Joined: 20 Oct 2012
Total Posts: 769
20 Jun 2014 01:03 PM
^^ I don't see you trying -_-
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
20 Jun 2014 01:05 PM
Color3 uses DECIMAL values, so divide the colors by 255.

Color3.new(150, 0, 0)

Should be

Color3.new(150/255, 0, 0)
Report Abuse
jakej78b is not online. jakej78b
Joined: 09 Mar 2011
Total Posts: 813
20 Jun 2014 01:05 PM
All of you that have just posted a while loop do not know how to code efficiently. I'm sorry, but it's true. [3] Learn to use events, idiots.
Report Abuse
smiley599 is not online. smiley599
Joined: 23 Jan 2010
Total Posts: 21869
20 Jun 2014 01:06 PM
Irrelevant.

Others have provided proper help and you provided nothing useful.
Report Abuse
jakej78b is not online. jakej78b
Joined: 09 Mar 2011
Total Posts: 813
20 Jun 2014 01:08 PM
I love how someone else just used that argument against you but you nullified it by saying they used bad practices now your siding with them, gg bye.
Report Abuse
Meowcatmaster is not online. Meowcatmaster
Joined: 20 Oct 2012
Total Posts: 769
20 Jun 2014 01:09 PM
It fun to make things short,cute and to the POINT not a long code.
Maybe you guys like it like that. I like make it as short as possible.
Report Abuse
Locard is not online. Locard
Joined: 13 Apr 2014
Total Posts: 3516
20 Jun 2014 01:10 PM
"Maybe you guys like it like that. I like make it as short as possible."

Short doesn't always mean efficient. I know I wouldn't want to have a while loop when I can just listen for an event instead.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image