Is There Something Wrong With This Script?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I have a game that has a treasure hunt event in Roblox which works by randomly spawning a gold treasure part at specified locations every five minutes. Each treasure remains visible for 30 seconds and features a label that says “Treasure!” above it.

  1. What is the issue? Include screenshots / videos if possible!
    i was hoping this script will work, but it seems that i did a somewhat mistake. and the script even breaks sometimes. in the output

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

i couldn’t really find something similar, nor do i know what’s causing this. its in a script in serverscriptservice.
if any of you have suggestions, please let me know
Script:

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local treasureLocations = {
    Vector3.new(10, 0, 20),
    Vector3.new(-15, 0, -30),
    Vector3.new(5, 0, 40),
}

local function spawnTreasure()
    local randomLocation = treasureLocations[math.random(1, #treasureLocations)]
    local treasure = Instance.new("Part")
    treasure.Size = Vector3.new(2, 2, 2)
    treasure.Position = randomLocation
    treasure.Anchored = true
    treasure.BrickColor = BrickColor.new("Gold")
    treasure.Parent = workspace
    
    local treasureLabel = Instance.new("BillboardGui", treasure)
    treasureLabel.Size = UDim2.new(0, 100, 0, 50)
    treasureLabel.Adornee = treasure 
    
    local labelText = Instance.new("TextLabel", treasureLabel)
    labelText.Text = "Treasure!"
    labelText.Size = UDim2.new(1, 0, 1, 0)
    labelText.BackgroundTransparency = 1

    wait(30)
    treasure:Destroy()
end

local function startEvent()
    while true do
        spawnTreasure()
        wait(300)
    end
end

startEvent()

What is the output of the code?

I have edited the code and added few debugs, lmk the results when you test.

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local treasureLocations = {
    Vector3.new(10, 0, 20),
    Vector3.new(-15, 0, -30),
    Vector3.new(5, 0, 40),
}

local function spawnTreasure()
    local randomLocation = treasureLocations[math.random(1, #treasureLocations)]
       local treasure = Instance.new("Part")
       treasure.Size = Vector3.new(2, 2, 2)
       treasure.Position = randomLocation
       treasure.Anchored = true
       treasure.BrickColor = BrickColor.new("Gold")
       treasure.Parent = workspace

       if not treasure then
          print("treasure is nil")
          return
       end
    
       local treasureLabel = Instance.new("BillboardGui", treasure)
       treasureLabel.Size = UDim2.new(0, 100, 0, 50)
       treasureLabel.Adornee = treasure
 
       if not treasureLabel then
          print("treasureLabel is nil")
          return
       end
    
       local labelText = Instance.new("TextLabel", treasureLabel)
       labelText.Text = "Treasure!"
       labelText.Size = UDim2.new(1, 0, 1, 0)
       labelText.BackgroundTransparency = 1

       if not labelText then
          print("labelText is nil")
          return
       end

       task.wait(30)
       treasure:Destroy()
end

local function startEvent()
    while task.wait() do
        spawnTreasure()
        task.wait(300)
    end
end

startEvent()

i wrote this on here not scripter editor i apologize if theres any syntax/spelling mistakes

oh my bad, it was actually my fault. i forgot to re insert the model (aka the treasure box) when i deleted it sorry everyone but now its working fine :wink:

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