Calling module from script breaks it

Hi, I recently added a module script (my first one ever) 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

That means tonumber couldnt convert your string into a number and it returned nil

1 Like

An easy fix to this is just to do:
local new = (tonumber(steps.Text) or 0)-1

This basically says if the converted string is nil, it uses 0 instead.

1 Like

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