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
We use cookies to offer you a better experience. By using Roblox.com, you are agreeing to our Privacy and Cookie Policy.
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Stupid scope

Previous Thread :: Next Thread 
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
25 Nov 2013 04:27 PM
I'm trying to make a model become a random color once someone touches it, but instead of the model becoming a different color, my head becomes a different color. When I've encountered this problem in the past, it was due to the fact that my local variable had used used outside its scope. The thing is, I use a for loop to do the GetChildren thing, and I'm not sure if that's local or not, and how to fix it so that either it's global, or to rearrange the script so that it works properly.

for i,v in pairs (script.Parent.Parent:GetChildren ()) do
function OnTouch (v)
local h = v.Parent:findFirstChild("Humanoid")
if h~=nil then
h.Health = 0
wait(1)
end
v.BrickColor = BrickColor.new (math.random ())
v.Transparency = math.random (0,1)
end
v.Touched:connect (OnTouch)
end

Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
25 Nov 2013 04:38 PM
How come this forum always dies once I post asking for help, but is so vibrant while i'm giving it?
Report Abuse
dmjoe is not online. dmjoe
Joined: 01 May 2009
Total Posts: 2387
25 Nov 2013 04:45 PM
par = script.Parent.Parent:GetChildren()
for x=1,#par do
par[x].Touched:connect(function(hit)
human = hit.Parent:findFirstChild("Humanoid")
if human then
human.Health = 0
wait(1)
par[x].BrickColor = BrickColor.new (math.random ())
par[x].Transparency = math.random (0,1)
end end)

Try this, not tested.
Report Abuse
dmjoe is not online. dmjoe
Joined: 01 May 2009
Total Posts: 2387
25 Nov 2013 04:45 PM
What is the output?
Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
25 Nov 2013 04:58 PM
No output, although I had to change the end a bit to make them match up. It only changes one of the bricks in the model, though.

par = script.Parent.Parent:GetChildren()
for x=1,#par do
par[x].Touched:connect(function(hit)
human = hit.Parent:findFirstChild("Humanoid")
if human then
human.Health = 0
wait(1)
par[x].BrickColor = BrickColor.new (math.random ())
par[x].Transparency = math.random (0,1)
end
end)
end
Report Abuse
dmjoe is not online. dmjoe
Joined: 01 May 2009
Total Posts: 2387
25 Nov 2013 05:06 PM
I don't think you can use an event inside a for loop like that, but I don't know an alternative.
Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
25 Nov 2013 09:43 PM
I know this might sound really bad, but:
define event, please.

Also, alternatives would be nice! Anyone think they have an answer?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Nov 2013 09:45 PM
You could do anything in a for loop...
But that, right there, is inefficient, do this

function OnTouch (v)
local h = v.Parent:findFirstChild("Humanoid")
if h~=nil then
h.Health = 0
wait(1)
end
v.BrickColor = BrickColor.new (math.random ())
v.Transparency = math.random (0,1)
end

for i,v in pairs (script.Parent.Parent:GetChildren ()) do
v.Touched:connect (OnTouch)
end
Report Abuse
chickenman158 is not online. chickenman158
Joined: 18 Jan 2011
Total Posts: 915
25 Nov 2013 09:49 PM
No output, although I had to change the end a bit to make them match up. It only changes one of the bricks in the model, though.

par = script.Parent.Parent:GetChildren()
for k, v in pairs(par) do
v.Touched:connect(function(hit)
human = hit.Parent:findFirstChild("Humanoid")
if human then
human.Health = 0
wait(1)
v.BrickColor = BrickColor.new (Color3.new(math.random(0,255)/255,math.random(0,255)/255,math.random(0,255)/255))
v.Transparency = math.random (0,1)
end
end)
end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Nov 2013 09:50 PM
v.BrickColor = BrickColor.Random()*
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Nov 2013 09:51 PM
I hate ninja's
And I hate flood check

Combine the two:
w33w00
Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
26 Nov 2013 06:55 AM
I really thought cntkillme's would work -I've seen him many times on this forum and he always gives good advice- but I found that chickenman's script worked.

There's just one problem: how do I get it so that all the bricks change to a random, yet uniform color?

I got some of them to be a uniform color, but some aren't:

par = script.Parent.Parent:GetChildren()
randombrick = script.Parent
for k, v in pairs(par) do
v.Touched:connect(function(hit)
human = hit.Parent:findFirstChild("Humanoid")
if human then
human.Health = 0
wait(1)

randombrick.BrickColor = BrickColor.new (Color3.new(math.random(0,255)/255,math.random(0,255)/255,math.random(0,255)/255))
v.BrickColor = randombrick.BrickColor

randombrick.Transparency = math.random (0.4,.8)
v.Transparency = randombrick.Transparency
end
end)
end
Report Abuse
frozendragon123 is not online. frozendragon123
Joined: 01 Feb 2013
Total Posts: 6
26 Nov 2013 07:41 AM
randombrick.Transparency = math.random (0.4,.8)
(0.4,.8)is it exist?
Report Abuse
Vaporation is not online. Vaporation
Joined: 08 Jun 2011
Total Posts: 1397
26 Nov 2013 07:48 AM
math.random(4, 8)/10
Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
26 Nov 2013 03:28 PM
Due to how it looks, I omitted it from the script, but I'll store that away for future use.

How do I get the bricks to have uniform, yet randomly chosen, BrickColor
Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
26 Nov 2013 04:23 PM
I could've sworn my method would work, but now only about half of them keep the uniform color.
Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
26 Nov 2013 05:46 PM
Going through a dry-spell, I see. I'm very close to finishing this game! Come on!
Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
26 Nov 2013 07:45 PM
No suggestions? At ALL?
Report Abuse
zZxTheBuilderxZz is not online. zZxTheBuilderxZz
Joined: 26 Oct 2013
Total Posts: 89
26 Nov 2013 08:36 PM
Idk how you would script this and it would probobly take a long time...

But anyways: Make one brick in model named BaseForChangeColor change color randomly. Then put a script in the rest of the bricks that checks the color of the BaseForChangeColor brick and change accordingly. But one thing, with my level of scripting i am thinking that it would be something like this:
If parent.parent.BaseForChangeColor.brickcolor == 1,1,1 then parent.brickcolor = 1,1,1
elseIf parent.parent.BaseForChangeColor.brickcolor == 2,1,1 then parent.brickcolor = 2,1,1
etc.etc.
Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
27 Nov 2013 09:27 AM
Haha, clever idea. Not the correct format at all, but lovely idea. I'll be posting how to do this properly as a thanks!
Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
27 Nov 2013 04:57 PM
It's poorly inefficient, but I ended up assigning all the bricks names, and doing it manually.

I still used the last replier's idea, so here's how I did it:

randombrick.Value = BrickColor.new (Color3.new(math.random(0,255)/255,math.random(0,255)/255,math.random(0,255)/255)) -- This is that outside value
part1.BrickColor = randombrick.Value
part2.BrickColor = randombrick.Value
part3.BrickColor = randombrick.Value
part4.BrickColor = randombrick.Value
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