substrata icon indicating copy to clipboard operation
substrata copied to clipboard

Help with some Luau code

Open JerryBerry12 opened this issue 11 months ago • 48 comments

Hey, Having trouble with this luau script. Basically I have a cube with the object ID 2314,then I have dummy cubes with object IDs 2313, 2315, 2316, 2317, 2318, 2319. This script isn't working. It's a game script, with coins. You have to dodge the cubes to get coins at the end. The error is: Lua error (ob 2314): [string "script"]:20: attempt to index nil with 'name' Can you help me? Thanks

--lua Script on "ObstacleSpawner" cube

local obstacleCount = 20
local spawnArea = {
    minX = -4,    -- Updated x range
    maxX = 23,    -- Updated x range
    minY = -4,    -- Updated y range
    maxY = 50,    -- Updated y range
    z = 0.5       -- Height (z) is always 0.5
}

-- List of obstacle UIDs
local obstacleUIDs = {2313, 2315, 2316, 2317, 2318, 2319}

local spawnedObstacles = {}  -- Store references to active obstacles

-- Function to manage coins per avatar (assuming objectStorage is available as per the docs)

function getCoinKey(avatar)
    return "coins_" .. getUserId(avatar.name)
end

function getCoins(avatar)
    local key = getCoinKey(avatar)
    local coins = objectStorage[key]
    if coins == nil then
        coins = 0
        objectStorage[key] = coins
    end
    return coins
end

function setCoins(avatar, amount)
    local key = getCoinKey(avatar)
    objectStorage[key] = math.max(0, amount)
end

function spendCoins(avatar, amount)
    local current = getCoins(avatar)
    setCoins(avatar, current - amount)
    -- Assuming there is a function to notify the user (which we might need to adjust)
    showMessageToUser("-" .. amount .. " coins. Remaining: " .. (current - amount))
end

function clearObstacles()
    -- Reset all obstacles back to a neutral position or hide them
    for i = 1, #spawnedObstacles do
        local obj = spawnedObstacles[i]
        local theobj = getObjectForUID(obj)  -- Correct way to fetch the object by UID
        theobj.pos = {x = 0, y = 0, z = 0.5}  -- Set position using pos directly
    end
    spawnedObstacles = {}
end

function spawnObstacles()
    clearObstacles()

    -- Randomly position the obstacles from the given UIDs
    for i = 1, obstacleCount do
        local x = math.random(spawnArea.minX, spawnArea.maxX)
        local y = math.random(spawnArea.minY, spawnArea.maxY)
        local z = spawnArea.z  -- z is always 0.5

        -- Pick a random obstacle from the provided UIDs
        local obstacleUID = obstacleUIDs[math.random(1, #obstacleUIDs)]  -- Randomly select an obstacle UID
        local theobj = getObjectForUID(obstacleUID)  -- Get the object using its UID
        theobj.pos = {x = x, y = y, z = z}  -- Set position using pos directly

        -- Add the coin-spending function to each obstacle when touched
        -- Use onUserUsedObject to detect interaction
        function onUserTouchedObject(avatar, object)
            spendCoins(avatar, 3)
        end
	addEventListener(onUserTouchedObject, obstacleUID, onUserTouchedObject())

        -- Add the obstacle to the list of spawned obstacles
        table.insert(spawnedObstacles, obstacleUID)
    end
end

-- Trigger when a user interacts with the object
function onUserUsedObject(avatar, object)
    -- When the user uses the object, trigger spawning obstacles
    spawnObstacles()
end

JerryBerry12 avatar Apr 21 '25 12:04 JerryBerry12

Hey, just following up.

JerryBerry12 avatar Apr 25 '25 19:04 JerryBerry12

Looks like avatar is nil for some reason

Ono-Sendai avatar Apr 26 '25 03:04 Ono-Sendai

Is this an error on the server or client?

Ono-Sendai avatar Apr 26 '25 03:04 Ono-Sendai

Server

JerryBerry12 avatar Apr 27 '25 23:04 JerryBerry12

In the main logs

JerryBerry12 avatar Apr 27 '25 23:04 JerryBerry12

Does the rest of the code seem valid?

JerryBerry12 avatar Apr 27 '25 23:04 JerryBerry12

Hey, just checking in.

JerryBerry12 avatar May 05 '25 12:05 JerryBerry12

It looks valid at first glance, the question is, does it work? :)

Ono-Sendai avatar May 06 '25 00:05 Ono-Sendai

Yeah that's my problem. I think ChatGPT is thinking that Luau is Roblox Luau which is a bit different. If I start from scratch, can you help me with what I'm trying to accomplish? I'll tell you all the details.

JerryBerry12 avatar May 07 '25 14:05 JerryBerry12

As you can tell, I don't have much experience with luau 😅

JerryBerry12 avatar May 07 '25 14:05 JerryBerry12

Yeah that's my problem. I think ChatGPT is thinking that Luau is Roblox Luau which is a bit different. If I start from scratch, can you help me with what I'm trying to accomplish? I'll tell you all the details.

sure, yeah can help

Ono-Sendai avatar May 07 '25 15:05 Ono-Sendai

Yeah that's my problem. I think ChatGPT is thinking that Luau is Roblox Luau which is a bit different. If I start from scratch, can you help me with what I'm trying to accomplish? I'll tell you all the details.

It's the same Luau. But roblox will have some different library functions.

Ono-Sendai avatar May 07 '25 15:05 Ono-Sendai

Yeah that's what I thought. I'll get back to you tomorrow about the project. We can continue here or email whichever.you prefer. I. Don't have discord

JerryBerry12 avatar May 07 '25 20:05 JerryBerry12

Ok here or discord is good.

Ono-Sendai avatar May 08 '25 15:05 Ono-Sendai

Here's a quick diagram of what it would look like. Don't feel like you have to code everything here for me, I just want some pointers on how I should go about doing it.

Image

JerryBerry12 avatar May 09 '25 12:05 JerryBerry12

I don't think cross-world scripting like that works.

Ono-Sendai avatar May 09 '25 12:05 Ono-Sendai

Wdym? Sorry if the answer is very obvious, I just dont understand

JerryBerry12 avatar May 09 '25 13:05 JerryBerry12

I mean that scripts in the main world can't access objects in the personal world. But maybe you don't need that.

Ono-Sendai avatar May 09 '25 13:05 Ono-Sendai

I don't think there will be one main script for everything. Probably one in the main world for getting info for the dashboard, and then one in the personal world for actually handling the race.

JerryBerry12 avatar May 09 '25 15:05 JerryBerry12

Can I use object storage to store scores in a certain object in the main world, then obtain those same scores from the personal world using that objects UID?

JerryBerry12 avatar May 09 '25 15:05 JerryBerry12

currently the object has to be in the same world

Ono-Sendai avatar May 09 '25 15:05 Ono-Sendai

You could do it using an external database and webserver btw since you can do HTTP calls

Ono-Sendai avatar May 09 '25 15:05 Ono-Sendai

Okay. Does luau have a way to send POST requests bc I need to update scores too.

JerryBerry12 avatar May 09 '25 16:05 JerryBerry12

Nvm i saw the docs and you can do post. What would the script look like if I were to do it that way?

JerryBerry12 avatar May 09 '25 16:05 JerryBerry12

You are running your own substrata server right? why not just do everything in 1 world?

Ono-Sendai avatar May 09 '25 16:05 Ono-Sendai

I could make a type of objectstorage that's separate from individual objects - that could be used across worlds

Ono-Sendai avatar May 09 '25 16:05 Ono-Sendai

maybe something like userstorage.setItem(key : string, value : Any serialisable Lua value)

Ono-Sendai avatar May 09 '25 16:05 Ono-Sendai

You're right, I'm totally overthinking this 😂. I'll put the track somewhere obscure in my main world. That way I could use object storage.

JerryBerry12 avatar May 09 '25 16:05 JerryBerry12

User storage is a cool idea tho! Just don't need it for this project

JerryBerry12 avatar May 09 '25 16:05 JerryBerry12

Unrelated question, just asking how does Screenshot bot work

JerryBerry12 avatar May 09 '25 17:05 JerryBerry12