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

What Should I Use as a Function Starter?

So Basically I am doing a level up system with experience, and when you reach certain levels I want the image of your new rank to show and the Text Label at the top, but I don't know what to use. I have tried player added and on enter, but it just keeps replaying every time it happens, and I don't know what to use.

Here is the code I am using right now.

nov = script.Parent.Main.Novice
app = script.Parent.Main.Apprentice
mas = script.Parent.Main.Master
name = script.Parent.Main.Title

function onEnter()
	if game.Players.LocalPlayer.Data.Level.Value == 2 then
		script.Parent.RankUp:Play()
		name:TweenPosition(UDim2.new(0.5, -200, 0, 0), "In", "Elastic", 0.1)
		wait(0.001)
		nov:TweenPosition(UDim2.new(0.5, -250, 0.5, -100), "In", "Quad", 0.3)
		wait(1.5)
		nov:TweenPosition(UDim2.new(-1, -250, 0.5, -100), "Out", "Quad", 0.3)
		wait(0.001)
		name:TweenPosition(UDim2.new(0.5, -200, -1, 0), "Out", "Quad", 0.1)
		wait(0.05)
	else
		nov:TweenPosition(UDim2.new(-1, -250, 0.5, -100), "Out", "Quad", 0.3)
		wait(0.05)
		name:TweenPosition(UDim2.new(0.5, -200, -1, 0), "Out", "Quad", 0.1)
		wait(0.05)
	end
	if game.Players.LocalPlayer.Data.Level.Value == 10 then
		script.Parent.RankUp:Play()
		name:TweenPosition(UDim2.new(0.5, -200, 0, 0), "Out", "Quad", 0.1)
		wait(0.001)
		mas:TweenPosition(UDim2.new(0.5, -250, 0.5, -100), "In", "Quad", 0.3)
		wait(1.5)
		mas:TweenPosition(UDim2.new(-1, -250, 0.5, -100), "Out", "Quad", 0.3)
		wait(0.001)
		name:TweenPosition(UDim2.new(0.5, -200, -1, 0), "Out", "Quad", 0.1)
		wait(0.05)
	else
		mas:TweenPosition(UDim2.new(-1, -250, 0.5, -100), "Out", "Quad", 0.3)
		wait(0.05)
		name:TweenPosition(UDim2.new(0.5, -200, -1, 0), "Out", "Quad", 0.1)
		wait(0.05)
	end
	if game.Players.LocalPlayer.Data.Level.Value == 20 then
		script.Parent.RankUp:Play()
		name:TweenPosition(UDim2.new(0.5, -200, 0, 0), "Out", "Quad", 0.1)
		wait(0.001)
		app:TweenPosition(UDim2.new(0.5, -250, 0.5, -100), "In", "Quad", 0.3)
		wait(1.5)
		app:TweenPosition(UDim2.new(-1, -250, 0.5, -100), "Out", "Quad", 0.3)
		wait(0.001)
		name:TweenPosition(UDim2.new(0.5, -200, -1, 0), "Out", "Quad", 0.1)
		wait(0.05)
	else
		app:TweenPosition(UDim2.new(-1, -250, 0.5, -100), "Out", "Quad", 0.3)
		wait(0.05)
		name:TweenPosition(UDim2.new(0.5, -200, -1, 0), "Out", "Quad", 0.1)
		wait(0.05)
	end
end

script.Parent.Main.MouseEnter:connect(onEnter)

Thanks!

1 answer

✓ Accepted answer

If you only want the function to be called when it changes, you're looking for the Changed event. You can also make a few edits to shorten it a bit:

local nov = script.Parent.Main.Novice --// Usage of local variables is recommended.
local app = script.Parent.Main.Apprentice
local mas = script.Parent.Main.Master
local name = script.Parent.Main.Title

local level = game.Players.LocalPlayer.Data.Level
local specialLevels = { --// Create a table to hold the levels which will trigger the animation.
	[2] = nov,
	[10] = app,
	[20] = mas,
};

level.Changed:connect(function(value) --// Connect a function to the level.Changed event.
	if specialLevels[value] then --// Check if the level is one of those levels..
		local obj = specialLevels[value];
		script.Parent.RankUp:Play()
		name:TweenPosition(UDim2.new(0.5, -200, 0, 0), "In", "Elastic", 0.1)
		wait(0.001) --// The lowest value will wait for is 0.03 seconds, just so you know.
		obj:TweenPosition(UDim2.new(0.5, -250, 0.5, -100), "In", "Quad", 0.3)
		wait(1.5)
		obj:TweenPosition(UDim2.new(-1, -250, 0.5, -100), "Out", "Quad", 0.3)
		wait(0.001)
		name:TweenPosition(UDim2.new(0.5, -200, -1, 0), "Out", "Quad", 0.1)
		wait(0.05)
	end;
end);

(I assumed that's what you wanted your function to do; if not you can just change the contents of the function to your original one)

Hope this helped.

Read more stuff: Tables, Anonymous Functions

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