How to store repititive stuff into a different script?

Hi, I wanted to make this topic because I have a game and there are 2 scripts in it with a lot of repititive stuff. What would be the best way to store it in one script and then access it in a local script? I am thinking module scripts but I have literally no experience with module scripts. Thanks!

Heres the repititive stuff if u want to know:
if stagetrans.Text == "1" then
		steps.Text = "100"
	elseif stagetrans.Text == "2" then
		steps.Text = "125"
	elseif stagetrans.Text == "3" then
		steps.Text = "125"
	elseif stagetrans.Text == "4" then
		steps.Text = "25"
	elseif stagetrans.Text == "5" then
		steps.Text = "200"
	elseif stagetrans.Text == "6" then
		steps.Text = "250"
	elseif stagetrans.Text == "7" then
		steps.Text = "110"
	elseif stagetrans.Text == "8" then
		steps.Text = "75"
	elseif stagetrans.Text == "9" then
		steps.Text = "75"
	elseif stagetrans.Text == "10" then
		steps.Text = "75"
	elseif stagetrans.Text == "11" then
		steps.Text = "125"
	elseif stagetrans.Text == "12" then
		steps.Text = "450"
	elseif stagetrans.Text == "13" then
		steps.Text = "200"
	elseif stagetrans.Text == "14" then
		steps.Text = "150"
	elseif stagetrans.Text == "15" then
		steps.Text = "200"
	end

so you have similar ones in the other script and you want to access then in local script ?

Yes i have the same exact thing in other scripts and they are all local scripts.

so you can create another script and use this method,
transfer all the data from the 1st 2 scripts to the new script you have made via a bindable functions and store in the new script.
Then you can access in from a local script

Wait I make them bindable functions? Also which script to store it into? (Module,local,normal)

Also how would I store them?

Maybe you can use a ModuleScript to store the data.

local module = {}
module.values = {
    ['1'] = 100;
    ['2'] = 125;
    ['3'] = 150;
}

function module.getVal(n:number)
    for i, v in module.values do
        if tostring(i) == tostring(n) then
            return v
        end
    end
end

return module
local module = require(game.ReplicatedStorage.ModuleScript)

steps.Text = module.getVal(stagetrans.Text)
2 Likes

A different way of doing it using a ModuleScript (which I think is more efficient) that @calvin_coco mentioned would be this:

local module = {
    ["1"] = 100;
    ["2"] = 125;
    ["3"] = 125;
    ["4"] = 25;
    ["5"] = 200;
    ["6"] = 250;
    ["7"] = 110;
    ["8"] = 75;
    ["9"] = 75;
    ["10"] = 75;
    ["11"] = 125;
    ["12"] = 450;
}

return module

Then to get the data, just include the module script and the variable will contain the code, like this:

local data = require(ModuleScript)
steps.Text = tostring(data[stagetrans.Text])

I do stuff like this quite a bit for static data that must be shared.

1 Like

Thanks so much but I have one problem, on another script I have, it is basically a safezone script and when i added “tostring(data[stagetrans.Text])”, it just broke and gives me an error on another script: Main:20: attempt to perform arithmetic (sub) on nil and number. If you could figure out this problem because I tried and did nothing it would be greatly appreciated :)) Here is my current code:

-- // Safe zone code \\

local SoundRegionsWorkspace = workspace.SafeZones
local plr = game.Players.LocalPlayer
local steps = plr.PlayerGui.StepsGUI.Frame.TextLabel
local stagetrans = plr.PlayerGui.StageTransfer.CurrentStage
local mod = script.Parent:WaitForChild('StoreSteps')
local data = require(mod)

function whileloop()
	while task.wait(1/30) do
		local Found = false
		for i, v in pairs(SoundRegionsWorkspace:GetChildren()) do
			local region = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))
			local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, plr.Character:GetDescendants())
			for _, part in pairs(parts) do
				-- Loop one by one through the parts table
				if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
					Found = true
					break
				end
			end
			if Found == true then
				break
			end
		end
		steps.Text = if Found or "nil" then "inf" else tostring(data[stagetrans.Text])
		break
	end
end

for _,v in pairs(SoundRegionsWorkspace:GetChildren()) do
	v.Touched:Connect(function()
		whileloop()
	end)
end


-- // Main \\

local player = game.Players.LocalPlayer
local character = player.Character
local Humanoid = player.Character:WaitForChild("Humanoid")
local steps = player.PlayerGui.StepsGUI.Frame.TextLabel
local stagetrans = player.PlayerGui.StageTransfer.CurrentStage
local leaderstats = player.leaderstats.Stage
local mod = script.Parent.StoreSteps
local RunService = game:GetService("RunService")
print(steps.Text)

local data = require(mod)

stagetrans:GetPropertyChangedSignal("Text"):Connect(function()
	steps.Text = tostring(data[stagetrans.Text])
end)

print('loop')
while task.wait(.1) do
	if Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) then
		local new = tonumber(steps.Text)-1 -- Error on this line
		local text = tostring(new)
		player.PlayerGui.StepsGUI.Frame.TextLabel.Text = text
		if new <= 15 then
			steps.TextColor3 = Color3.new(0.866667, 0, 0.0156863)
			steps.Parent.ImageLabel.ImageColor3 = Color3.new(0.866667, 0, 0.0156863)
		elseif new >= 16 then
			steps.TextColor3 = Color3.new(255,255,255)
			steps.Parent.ImageLabel.ImageColor3 = Color3.new(255,255,255)
		end
		if new <= 0 then
			Humanoid.Health -=100
			steps.Text = tostring(data[stagetrans.Text])
			break
		end
	end
end

You can’t do that. Do this instead…

if Found then
	steps.Text = "inf"
else
	steps.Text =  tostring(data[stagetrans.Text])
end

Yes but that then gives an error.

Go back and look at my code. You cannot make something equal to an if statement.

No, you actually can do that. It works like a normal if statement but it doesn’t need an end.

local label = script.Parent.TextLabel
local bool = script.BoolValue

label.Text = if bool.Value then 'yes' else 'no' -- But you can't do this on multiple lines
-- elseif also works
2 Likes

You are the only one that I have seen do that. It’s like the ternary statement in C/C++ and other languages where you have variable = (condition) ? true : false;. I’ll have to try it to see it for myself.

You’re doing the statement wrong
It would be

steps.Text = if Found then "inf" else tostring(data[stagetrans.Text])

OR

steps.Text = Found and "inf" or tostring(data[stagetrans.Text])

You are absolutely correct and I stand corrected. You can do that. I learned something new today.

local bool = false
local value = if bool then 12 else 6

is perfectly valid code.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.