Help with some Luau code
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
Hey, just following up.
Looks like avatar is nil for some reason
Is this an error on the server or client?
Server
In the main logs
Does the rest of the code seem valid?
Hey, just checking in.
It looks valid at first glance, the question is, does it work? :)
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.
As you can tell, I don't have much experience with luau 😅
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
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.
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
Ok here or discord is good.
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.
I don't think cross-world scripting like that works.
Wdym? Sorry if the answer is very obvious, I just dont understand
I mean that scripts in the main world can't access objects in the personal world. But maybe you don't need that.
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.
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?
currently the object has to be in the same world
You could do it using an external database and webserver btw since you can do HTTP calls
Okay. Does luau have a way to send POST requests bc I need to update scores too.
Nvm i saw the docs and you can do post. What would the script look like if I were to do it that way?
You are running your own substrata server right? why not just do everything in 1 world?
I could make a type of objectstorage that's separate from individual objects - that could be used across worlds
maybe something like
userstorage.setItem(key : string, value : Any serialisable Lua value)
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.
User storage is a cool idea tho! Just don't need it for this project
Unrelated question, just asking how does Screenshot bot work