Calling module from script breaks other script

Did it work?

No it did not I will send video of whats happening now


@dudesa3000

Luau has if-then-else statements so local x = if a then b else c is valid syntax.

1 Like

That is true but. If statements do not work for assigning values to a variable.

1 Like
if a then
    local x = b
else 
    local x = c
end

is the same as

local x = a and b or c
1 Like

It does in Luau, there is official documentation on it as well.

local x = if true then 2 else false

It’s advised to use if-then-else expressions over a and b or c as if you’re relying on the value to be something falsy other than false (nil), you won’t get expected results.

local a = b and 'something' or nil

if b is false for example, a will be assigned as false instead of nil whereas

local a = if b then 'something' else nil

will allow ‘a’ to be nil.

We’re straying off topic though, so send me a DM if you need clarification.

1 Like

I am so sorry; I didn’t know that. I only thought that because I tried to do something like it before and it didn’t work.

2 Likes

Hey guys, sorry to butt in, love the chitchat but can we stay on topic here lol

EVERYONE!! I FIXED IT!

Problem: There was no [“0”] variable in the module script so when the player was in lobby the stage transfer text was “0”, so the script couldnt find 0 and returned it as nil.

1 Like

@OP, a few things:
1- You are breaking your loop (I think) prematurely, if you want an infinite loop, you are going to have your loop only iterate one time so you should remove the break statement.
2- steps.Text is being assigned a as “inf” always as the string “nil” is truthy. I think you should be checking if found == nil then "inf" else data[stagetransfer.Text]
You can clean it up a bit by doing
if found then data[stagetransfer.Text] else "inf"
3- Region3’s spatial methods are deprecated so you might find unexpected results. You should maybe try using the spatial query API

4- You aren’t handling the case where steps.Text cannot be converted to a number

Then when iterating through, you can use players:GetPlayerFromCharacter on the part’s parent to see if the part belongs to the LocalPlayer.

So all in all your final code would look something like this:

local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Whitelist
overlapParams.FilterDescendantsInstances = { localPlayer.Character }

-- we should handle when the player's character is added if this script isn't inside of the actual character:
--[[
localPlayer.CharacterAdded:Connect(function(newCharacter)
    overlapParams.FilterDescendantsInstances = { newCharacter }
end)
]]

while true do
    local foundCharacter = false
    for _, child in ipairs(soundRegions:GetChildren()) do
        if not child:IsA('BasePart') then continue end -- if v isn't a part we don't want to check it as it will error
            local foundParts = workspace:GetPartsInPart(child, overlapParams) -- like Region3:FIndPartsInRegion3WithWhiteList, it returns a table of parts so your loop will essentially be identical
            -- here, loop through foundParts, so for i,v in ipairs(foundParts) do
            if foundCharacter then
                break
            end
            steps.Text = if found and data[stagetrans.Text] then data[stagetrans.Text] else 0 
            --[[ so here on the previous line, we need to check if the part was found and if 'data' (which I 
assume is a table) [stagetrans.Text] exists. if not, steps.Text will be assigned as '0' which is necessary 
below. also generally when assigning numbers to string arguments or vice versa, tonumber/tostring is 
either called on the backend or isn't necessary so assigning a number to a string property is always 
okay. ]]
        end
    end
    task.wait(1 / 30)
end

Also, for this, you do not need a .Touched connection as you’re going to have a memory leak as a new loop is created every time a part is touched. You might want to look into coroutines.

Anyway to continue,

while true do
    if humanoid.MoveDirection.Magnitude > 0.0005 then -- we want to use 0.0005 to account for floating errors (not an actual Lua error) as humanoid.MoveDirection might not always be equal to exactly 0, 0, 0
        -- now, in the case that steps.Text is not a number, we want to handle that.
        -- so if steps.Text can be converted to a number, then we can do steps.Text - 1 to subtract a number. doing math on strings that can be converted to numbers is also okay.
        -- if it can't be converted to a number, it gets assigned as 0.
        local new = if tonumber(steps.Text) then steps.Text - 1 else 0
        if new <= 15 then
            steps.TextColor3 = Color3.new(...)
            steps.Parent.ImageLabel.ImageColor3 = Color3.new(...)
        else
            do the rest of the if statement here
        end
        if new <= 0 then
            -- again do the rest of the if-statement
        end
    end
    task.wait(1 / 30)
end
1 Like

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