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

How to fix Attempt to index nil with error in this script??

For some reason in the script, it says Attempt to index nil with 'glow' I have tried using if statements to counter this but if statements don't work.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local changer = ReplicatedStorage:WaitForChild("ColorChanger")
print("Remote event obtained")

workspace.DescendantAdded:Wait("Star")

changer.OnServerEvent:Connect(function(player)
	local cloneStar = game.Workspace:FindFirstChild("Star")
	print("Start")
	
	
	local Colors = {BrickColor.new("Pastel yellow").Color, BrickColor.new("Cool yellow").Color, BrickColor.new("Daisy orange").Color, BrickColor.new("Bright yellow").Color, BrickColor.new("New Yeller").Color, BrickColor.new("Deep orange").Color} 
	
	
	local TweenService = game:GetService("TweenService")
	
	
	local function ColorChange(ColorToChangeTo)
		print("Function on")
		
			
		local Union = cloneStar.glow
			
		local tweenInfoColor = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
		local ColorProperty = {}
		
		ColorProperty.Color = ColorToChangeTo
		
		local tween = TweenService:Create(Union,tweenInfoColor,ColorProperty)
		tween:Play()
		print("Tween playing")
		
					
		wait(1)
	
	end
	
	
	while true do	
		
		for i,v in ipairs(Colors) do
													
			ColorChange(v)
			
		end		
							
	end
			
end)

5 comments

0

workspace.DescendantAdded:Wait("Star"). Doesn't work like that my man.

0

The :Wait method of RBXScriptSignal merely holds the thread until the Event fires, or the given* timeout is exceeded. You need to ensure your 'cloneStar' Instance exists before proceeding with it.

0

Oh, Imma delete it and also I tried that using an if statement and it didn't work for some reason

0

'if (cloneStar ~= nil) then'?

0

Yep, doesn't work for some reason, if you wanna see the code with the changes I can post it.

2 answers

✓ Accepted answer

Try this, I've just added some error checks....

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local changer = ReplicatedStorage:WaitForChild("ColorChanger",10)
print("Remote event obtained")

--[[ this was
workspace.DescendantAdded:Wait("Star")
]]
local Parent = workspace -- Change this to where you have your star object!
local cloneStar = Parent:WaitForChild("Star",10)

--// enable this if you want the script to stop if it cannot find the star object
--if(cloneStar == nil) then error("Star Cannot be found in ["..Parent:GetFullName().."]!! \n stopping script!!") end

changer.OnServerEvent:Connect(function(player)
    print("Start")

    local Colors = {BrickColor.new("Pastel yellow").Color, BrickColor.new("Cool yellow").Color, BrickColor.new("Daisy orange").Color, BrickColor.new("Bright yellow").Color, BrickColor.new("New Yeller").Color, BrickColor.new("Deep orange").Color} 
    local TweenService = game:GetService("TweenService")
    local function ColorChange(ColorToChangeTo)
-- 		local cloneStar = game.Workspace:FindFirstChild("Star")
		if(cloneStar ~= nil) then 
			print("Function on")
	
	
			local Union = cloneStar.glow
	
			local tweenInfoColor = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
			local ColorProperty = {}
	
			ColorProperty.Color = ColorToChangeTo
	
			local tween = TweenService:Create(Union,tweenInfoColor,ColorProperty)
			tween:Play()
			print("Tween playing")
		else
			--// Comment this out if you don't want this spamming in your output
			warn("Warning Cannot Find cloneStar in "..tostring(Parent).." Please check your code and try again!")
		end
		wait(1)
    end

    while true do   
        for i,v in ipairs(Colors) do
            ColorChange(v)
        end     
    end
end)

I know it isn't much, but it might help you squish that bug! :)

12 comments

0

Testing it right now but I am getting an error with error("Star Cannot be found in [",Parent:GetFullName(),"]!! \n stopping script!!"), it says(number expected, got string)

0

Also what is the 10 for in line 9??

0

For some reason the if statement just dowsn't work, that's the only problem.

0

strange... Is there anything above the error?. Sometimes it does list everything after an error as an error, The 10 in the WaitForchild is the time out in seconds if it finds the thing your looking for it stops early otherwise it will wait the seconds you specify.

0

i also did the changes in notapad++ not roblox so i may have missed something. ill check the code :)

0

Found the error the error() function cannot have commas ( , ) unless you're specifying an error code Duh @ me lol Fixed script

0

Note to self: Test Scripts Before Submitting!.... lol >_>

0

Sorry for the late reply, don't worry about the mistakes it happens lol and also now I understand what the 10 is for. No, everything above the if statement works until I reach the if statement unless line 25 is the real issue.

0

In line 25 I am trying to get a part within the Star model so I don't know if that is the issue besides the if statement.

0

The only issue is that the script for some reason just can't find the cloned model in workspace. Maybe because I am cloning it on a local scipt??

0

I just need to get it to find the cloned model in workspace

0

I made it into a local script.

Figured it out, I just used a local script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local arrow = ReplicatedStorage:WaitForChild("Arrow")


arrow.OnClientEvent:Connect(function()
	local cloneStar = workspace:FindFirstChild("Star")
	print("Obtained")
	
	if cloneStar == nil or cloneStar["glow"] == nil then			
		error("Star Cannot be found in ["..workspace:GetFullName().."]!! \n stopping script!!")
	end
	
	
	local Colors = {BrickColor.new("Pastel yellow").Color, BrickColor.new("Cool yellow").Color, BrickColor.new("Daisy orange").Color, BrickColor.new("Bright yellow").Color, BrickColor.new("New Yeller").Color, BrickColor.new("Deep orange").Color} 

	
	local TweenService = game:GetService("TweenService")

	
	local function ColorChange(ColorToChangeTo)
		if cloneStar ~= nil or cloneStar["glow"] ~= nil then 		
			print("Function on")
			
			local Union = cloneStar["glow"]
				
			local tweenInfoColor = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
			local ColorProperty = {}

			ColorProperty.Color = ColorToChangeTo

			local tween = TweenService:Create(Union,tweenInfoColor,ColorProperty)		
			tween:Play()	
			print("Tween playing")	
		else					
			warn("Warning Cannot Find cloneStar in "..tostring(workspace).." Please check your code and try again!")
		end
	
		wait(1)
		
	end
	
	
	while true do
		
		for i,v in ipairs(Colors) do
					
			ColorChange(v)
							
		end 		
	
	end
	
end)

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