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

Making a Gui function with arrow keys through a LocalScript, help?

For some reason the LocalScript is not running. I presume it's because of the functions I used, but I eagerly want to make this script work. For a valid reason.

Please note that "Orange Text" is an ImageLabel , "OrangeTextLabelHitValue" is an
IntValue that tells when the "Orange Text" ImageLabel is supposed to move. ControlFrame is just an ordinary Frame

Structure

-Frame

   ~~OrangeTextHitValue

    ~~ControlGui <--  This is where the LocalScript I'm talking about is located.**

   ~~OrangeText
local ControlFrame = script.Parent
local OrangeTextLabel = ControlFrame:WaitForChild("OrangeText") 
local ControlTrigger = ControlFrame:WaitForChild("OrangeTextHitValue").Value

ControlTrigger.Changed:Connect(function()
if ControlTrigger == 1 then
ControlFrame.Position = UDim2.new(0,0,0,0)
OrangeTextLabel.Position = UDim2.new(0,0,0,0)

end	
end)

ControlTrigger.Changed:Connect(function()
if ControlTrigger == 2 then
ControlFrame.Position = UDim2.new(0,0,55,0)
OrangeTextLabel.Position = UDim2.new(0,0,-55,0)

end	
end)

ControlTrigger.Changed:Connect(function()
if ControlTrigger == 3 then
ControlFrame.Position = UDim2.new(0,0,110,0)
OrangeTextLabel.Position = UDim2.new(0,0,-110,0)

end	
end)

ControlTrigger.Changed:Connect(function()
if ControlTrigger == 4 then
ControlFrame.Position = UDim2.new(0,0,165,0)
OrangeTextLabel.Position = UDim2.new(0,0,-165,0)

end	
end)

ControlTrigger.Changed:Connect(function()
if ControlTrigger == 4 then
ControlFrame.Position = UDim2.new(0,0,220,0)
OrangeTextLabel.Position = UDim2.new(0,0,-220,0)

end	
end)

1 answer

✓ Accepted answer

Hey JoeRaptor,

The mistake in your script is quiet blatant however, there are also some enhancements you need on your script. Here, I'll go over the tiny issue you have first off.

#Issue: Updating

###Alright, so your issue that I noticed is you made a variable for the value itself. That's not very good because, you can't do events on numbers, you must do them on UserDataValues. You need to set a variable for the UserDataValue which is the 'IntValue' and then connect it to the .Changed event. Which, also has the parameter as the value that it has changed to. I'll show you what I mean below.

####Mistake

local ControlTrigger = ControlFrame:WaitForChild("OrangeTextHitValue").Value; -- Now it has the value in it stored.

ControlTrigger.Changed:Connect(function(val)
	-- First off, this won't even run because the value is constant and events can't be used on numbers like that, it must be used on userdata values. 
end)

###I'll show you below how you can avoid this.

####Corrected

local ControlTrigger = ControlFrame:WaitForChild("OrangeTextHitValue");

ControlTrigger.Changed:Connect(function(val) -- val is the number that it has become.
	if val == 1 then 
		-- blah blah blah
	end
end)

#Enhancement: DRY ###Now that we've went over the issue as you can see from above, I'll go over the enhancements you need in your script. First off, remember, DRY(Don't Repeat Yourself).

###In your script, you made many functions. That's not very good because they are very unnecessary, redundant, make it harder to read, and they can increase latency in your game. Here, I'll show you how you can easily wrap all that into one function and make it work perfectly.

####Enhancement DRY

local ControlTrigger = ControlFrame:WaitForChild("OrangeTextHitValue");

ControlTrigger.Changed:Connect(function(val)
	if val == 1 then
		-- blah
	else if val == 2 then
		-- blah blah
		end
	else if val == 3 then
		-- blah blah blah
	else if val == 4 then
		-- blah blah blah blah
	end
	end
	end
	end
end)

###Bam, that's how you'd simply wrap all of those into one function.

##Well, I hope I helped and have a wonderful day/night.

~~ KingLoneCat

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