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

Roblox error - Dunno how to fix?

Asked by
KordGamer 155
9 years ago

I'm working on a game and I made a morph when you click a gui button.

Here is the script:

local Suit = game.Lighting:findFirstChild(script.Parent.Suit.Value) -- finds the suits
local Me = script.Parent.Parent.Parent.Parent.Parent.Character -- finds you
local Weapon = game.Lighting:findFirstChild(script.Parent.Weapon.Value) -- Give You A Weapon
local Weapon1 = game.Lighting:findFirstChild(script.Parent.Weapon1.Value) -- Give You A Weapon
local Weapon2 = game.Lighting:findFirstChild(script.Parent.Weapon2.Value) -- Give You A Weapon
local Weapon3 = game.Lighting:findFirstChild(script.Parent.Weapon3.Value) -- Give You A Weapon




local debounce = false
local Me2 = script.Parent.Parent.Parent.Parent.Parent.Backpack -- this puts the weapon into backpack
local Me3 = script.Parent.Parent.Parent.Parent.Parent.PlayerGui -- this puts the weapon into backpack


function getPlayer(humanoid) 
local players = game.Players:children() 
for i = 1, #players do 
if players[i].Character.Humanoid == humanoid then return players[i] end 
end 
return nil 
end 



function Click2(mouse)
-- Speed, Weapons, and Health Give in this Function
local human = Me:findFirstChild("Humanoid") 
if (human ~= nil) and debounce == false then

debounce = true

local player = getPlayer(human) 

if (player == nil) then return end 

--[[Weapon:clone().Parent = Me2 -- this makes when you click this GUI it weilds on you
Weapon1:clone().Parent = Me2 -- these weapons go into your backpack (not auto weld like above)
Weapon2:clone().Parent = Me2
Weapon3:clone().Parent = Me3
]]--
wait(1)
debounce = false
script.Parent.Parent.Visible = false
human.WalkSpeed = script.Parent.Speed.Value -- changes your WalkSpeed to the "Speed" Value
human.MaxHealth = script.Parent.Health.Value -- changes your MaxHealth to the "Health" Value
human.Health = script.Parent.Health.Value
local hat = Suit.BrownRoblohunk:Clone()
hat.Parent = Me
local hat2 = Suit.SteampunkCadetHat:Clone()
 hat2.Parent = Me
Me.Head.face:remove()
local newface = Suit.face:Clone()
newface.Parent = Me.Head
Me.Shirt:remove()
Me.Pants:remove()
local Shirt = Suit.Shirt:Clone()
local Pants = Suit.Pants:Clone()
Shirt.Parent =  Me
Pants.Parent = Me-- changes your MaxHealth to the "Health" Value
end


end


script.Parent.MouseButton1Down:connect(Click2)

and here is the error:

(line 48) attempt to index upvalue 'Suit' (a nil value)

Help needed ASAP. Thank you!

2 answers

Log in to vote
1
Answered by 9 years ago
local Suit = game.Lighting:WaitForChild(script.Parent.Suit.Value) --assuming script.Parent is a string value
local Me = script.Parent.Parent.Parent.Parent.Parent.Character -- finds you
local Weapon = game.Lighting:findFirstChild(script.Parent.Weapon.Value) -- Give You A Weapon
local Weapon1 = game.Lighting:findFirstChild(script.Parent.Weapon1.Value) -- Give You A Weapon
local Weapon2 = game.Lighting:findFirstChild(script.Parent.Weapon2.Value) -- Give You A Weapon
local Weapon3 = game.Lighting:findFirstChild(script.Parent.Weapon3.Value) -- Give You A Weapon




local debounce = false
local Me2 = script.Parent.Parent.Parent.Parent.Parent.Backpack -- this puts the weapon into backpack
local Me3 = script.Parent.Parent.Parent.Parent.Parent.PlayerGui -- this puts the weapon into backpack


function getPlayer(humanoid) 
local players = game.Players:children() 
for i = 1, #players do 
if players[i].Character.Humanoid == humanoid then return players[i] end 
end 
return nil 
end 



function Click2(mouse)
-- Speed, Weapons, and Health Give in this Function
local human = Me:findFirstChild("Humanoid") 
if (human ~= nil) and debounce == false then

debounce = true

local player = getPlayer(human) 

if (player == nil) then return end 

--[[Weapon:clone().Parent = Me2 -- this makes when you click this GUI it weilds on you
Weapon1:clone().Parent = Me2 -- these weapons go into your backpack (not auto weld like above)
Weapon2:clone().Parent = Me2
Weapon3:clone().Parent = Me3
]]--
wait(1)
debounce = false
script.Parent.Parent.Visible = false
human.WalkSpeed = script.Parent.Speed.Value -- changes your WalkSpeed to the "Speed" Value
human.MaxHealth = script.Parent.Health.Value -- changes your MaxHealth to the "Health" Value
human.Health = script.Parent.Health.Value
local hat = Suit.BrownRoblohunk:Clone()
hat.Parent = Me
local hat2 = Suit.SteampunkCadetHat:Clone()
 hat2.Parent = Me
Me.Head.face:remove()
local newface = Suit.face:Clone()
newface.Parent = Me.Head
Me.Shirt:remove()
Me.Pants:remove()
local Shirt = Suit.Shirt:Clone()
local Pants = Suit.Pants:Clone()
Shirt.Parent =  Me
Pants.Parent = Me-- changes your MaxHealth to the "Health" Value
end


end


script.Parent.MouseButton1Down:connect(Click2)

You were using findFirstChild where that would return nil if it didn't find the child, with WaitForChild it will wait until it finds the child :)

Ad
Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

FindFIrstChild() will only return the object if it currently exists. Otherwise it returns nil. This is your problem. FindFIrstChild is returning nil, which is why you get the error.

Instead of FindFIrstChild, use WaitForChild. This waits for the child to exist, then returns it, instead of immediately returning nil if it doesn't currently exist.

local Suit = game.Lighting:WaitForChild(script.Parent.Suit.Value)

You would want to do this for your other variables as well.

Answer this question