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 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
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)
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
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
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.