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

How would I go about using a string to make this level up script easier to configure?

Asked by 6 years ago

I have created a level up script for the GUI so the actual level up script fires the client when you level up.

But the problem is I have to add about 10 lines for every level up so id end up with this huge level up script that would just be a pain.

So I am wondering if there is some way to make it using a string or something similar, heres my code.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local UnlockOrLevelUp = ReplicatedStorage:WaitForChild("UnlockOrLevelUp")

local Player = game.Players.LocalPlayer

function UnlockOrLevelUpEvent()
    -------------------------------------------------------------------------- Level 1
    if ReplicatedStorage.DataFile[Player.Name].PlayerLevel.Value == 1 then
        game.Workspace.AudioStorage.UnlockOrLevelUpSound:clone().Parent = SoundService
        SoundService:PlayLocalSound(SoundService:WaitForChild("UnlockOrLevelUpSound"))
        script.Parent.MainFrame.LevelUp.Text = "YOU HAVE LEVELED UP"
        script.Parent.MainFrame.LevelUp.SubText.Text = "LEVEL 1"
        script.Parent.MainFrame.LevelUp:TweenPosition(UDim2.new(.5, -200, .1, -25), "Out", "Quad", .5)
        wait(4)
        script.Parent.MainFrame.LevelUp:TweenPosition(UDim2.new(1.5, -200, .1, -25), "In", "Quad", 1)
    -------------------------------------------------------------------------- Level 2
    elseif ReplicatedStorage.DataFile[Player.Name].PlayerLevel.Value == 2 then
        game.Workspace.AudioStorage.UnlockOrLevelUpSound:clone().Parent = SoundService
        SoundService:PlayLocalSound(SoundService:WaitForChild("UnlockOrLevelUpSound"))
        script.Parent.MainFrame.LevelUp.Text = "YOU HAVE LEVELED UP"
        script.Parent.MainFrame.LevelUp.SubText.Text = "LEVEL 2"
        script.Parent.MainFrame.LevelUp:TweenPosition(UDim2.new(.5, -200, .1, -25), "Out", "Quad", .5)
        wait(4)
        script.Parent.MainFrame.LevelUp:TweenPosition(UDim2.new(1.5, -200, .1, -25), "In", "Quad", 1)
    -------------------------------------------------------------------------- Level 3
    elseif ReplicatedStorage.DataFile[Player.Name].PlayerLevel.Value == 3 then
        game.Workspace.AudioStorage.UnlockOrLevelUpSound:clone().Parent = SoundService
        SoundService:PlayLocalSound(SoundService:WaitForChild("UnlockOrLevelUpSound"))
        script.Parent.MainFrame.LevelUp.Text = "YOU HAVE LEVELED UP"
        script.Parent.MainFrame.LevelUp.SubText.Text = "LEVEL 3"
        script.Parent.MainFrame.LevelUp:TweenPosition(UDim2.new(.5, -200, .1, -25), "Out", "Quad", .5)
        wait(4)
        script.Parent.MainFrame.LevelUp:TweenPosition(UDim2.new(1.5, -200, .1, -25), "In", "Quad", 1)
    end
end

UnlockOrLevelUp.OnClientEvent:connect(UnlockOrLevelUpEvent)

Thats all the code just for 3 levels! So surely you under stand why I am trying to figure out a way to clean this up.

Thank you for your time :)

1 answer

Log in to vote
1
Answered by 6 years ago

Fixing that is rather easy, let me explain.

Instead of looking for what level you are it should just execute the code without the "if" lines. And then add up to the string saying "LEVEL 1" instead do

 script.Parent.MainFrame.LevelUp.Text = "LEVEL "..ReplicatedStorage.DataFile[Player.Name].PlayerLevel.Value

After those changes the code will look like this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local UnlockOrLevelUp = ReplicatedStorage:WaitForChild("UnlockOrLevelUp")

local Player = game.Players.LocalPlayer

function UnlockOrLevelUpEvent()  
        game.Workspace.AudioStorage.UnlockOrLevelUpSound:clone().Parent = SoundService
        SoundService:PlayLocalSound(SoundService:WaitForChild("UnlockOrLevelUpSound"))
        script.Parent.MainFrame.LevelUp.Text = "YOU HAVE LEVELED UP"
        script.Parent.MainFrame.LevelUp.SubText.Text = "LEVEL "..ReplicatedStorage.DataFile[Player.Name].PlayerLevel.Value
        script.Parent.MainFrame.LevelUp:TweenPosition(UDim2.new(.5, -200, .1, -25), "Out", "Quad", .5)
        wait(4)
        script.Parent.MainFrame.LevelUp:TweenPosition(UDim2.new(1.5, -200, .1, -25), "In", "Quad", 1)
end

Please tell me if you find any errors. //0skarian

Ad

Answer this question