-
What do you want to achieve?
Attempting to have a randomized spawn system for an item/tool in my game. -
What is the issue?
Items seem to only be spawning in one randomized location but not the other. I’m not getting any output errors; however, I don’t think it’s working the way I’d like it to. -
What solutions have you tried so far?
I’ve rewritten the part of the module script many times. I’ve tried to find videos too but have not been able to find anything.
Details
Workspace Set-up Screenshots
This is what my workspace looks like.
General overview
Location Attachment set-up
Where the item/Key is located w/in the Workspace
Scripts
Workspace Set-up Screenshot
Workspace script Overview
Furniture script
This is the script that I believe the issue is coming from :confused:local TweenService = game:GetService("TweenService")
local furniture = {}
local closet = require(script.Closet)
function furniture.OpenDrawer(drawer)
drawer:SetAttribute("Moving", true)
local isOpen = drawer:GetAttribute("Open")
local direction = isOpen and 1 or -1
local cframe = drawer.CFrame * CFrame.new(0,0,2 * direction)
local drawerTween = TweenService:Create(drawer, TweenInfo.new(0.5),{CFrame =cframe})
drawer.Move:Play()
drawerTween:Play()
drawerTween.Completed:Wait()
drawer:SetAttribute("Moving", false)
drawer:SetAttribute("Open", not isOpen)
end
function furniture.New (template, roomModel)
local furnitureModel = workspace.Furniture:FindFirstChild(template.Name)
if furnitureModel then
furnitureModel = furnitureModel:Clone()
furnitureModel:PivotTo(template.CFrame)
local itemLocations = {}
--I believe I'm having my issue here vv
if furnitureModel:FindFirstChild("CDesk") then
for i, desk in ipairs(furnitureModel.Desk:GetChildren()) do
table.insert(itemLocations, desk.Location)
end
end
-- I believe I'm having my issue here ^^
if furnitureModel:FindFirstChild("Drawers") then
for i, drawer in ipairs(furnitureModel.Drawers:GetChildren()) do
table.insert(itemLocations, drawer.Location)
drawer:SetAttribute("Open", false)
drawer:SetAttribute("Moving", false)
local prompt = Instance.new("ProximityPrompt")
prompt.ActionText = ""
prompt.MaxActivationDistance = 5
prompt.Parent = drawer.Toggle
prompt.Style = "Custom" --added
prompt.Triggered:Connect(function()
if drawer:GetAttribute("Moving") == false then
furniture.OpenDrawer(drawer)
end
end)
end
end
furnitureModel.Parent = template.Parent
template:Destroy()
return itemLocations
end
end
function furniture.FurnishRoom(roomModel)
local roomItemLocations = {}
if roomModel:FindFirstChild("Furniture") then
local templates = roomModel.Furniture:GetChildren()
for i, part in ipairs(templates) do
if part.Name == "Locker" then
--create new closet
closet.New(part)
else
local locations = furniture.New(part, roomModel)
if locations then
for index, value in ipairs(locations) do
table.insert(roomItemLocations, value)
end
end
end
end
end
if #roomItemLocations > 0 then
return roomItemLocations
end
end
return furniture
Room script
This script could be related to the issue so I thought I’d provide it
local TweenService = game:GetService("TweenService")
local door = require(script.Door)
local furniture = require(script.Furniture)
local item = require(script.Item)
local room = {}
room.info = require(script.RoomInfo)
room.lastTurnDirection = nil
room.random = Random.new()
function room.FlickerLight(lightPart)
local info = TweenInfo.new(0.2, Enum.EasingStyle.Elastic, Enum.EasingDirection.InOut, 1, false)
local flickOn = TweenService:Create(lightPart.PointLight, info, {Brightness = .5})
local flickOff = TweenService:Create(lightPart.PointLight, info, {Brightness = 0})
flickOff:Play()
flickOff.Completed:Wait()
lightPart.Material = Enum.Material.Glass
flickOn:Play()
flickOn.Completed:Wait()
lightPart.Material = Enum.Material.Neon
flickOff:Play()
flickOff.Completed:Wait()
lightPart.Material = Enum.Material.Glass
end
function room.Blackout (roomModel)
local lights = roomModel.Lights:GetChildren()
for i, light in ipairs(lights) do
for i, obj in ipairs(light:GetChildren()) do
if obj.Name == "Shade" then
task.spawn(function()
room.FlickerLight(obj)
end)
end
end
end
end
function room.GetRandom(PrevRoom)
local totalWeight = 0
for i, info in pairs(room.info) do
totalWeight += info.Weight
end
local randomWeight = room.random:NextNumber(0, totalWeight)
local currentWeight = 0
local randomRoom = nil
for i, info in pairs(room.info) do
currentWeight += info.Weight
if randomWeight <= currentWeight then
randomRoom = workspace.Rooms[i]
break
end
end
--[1]Nxt room must be dif than prev room
--[2]if corner then next turn other way
--[3]if prev room = stairs, then next cannot
local direction = room.info[randomRoom.Name]["Direction"]
local hasStairs = room.info[randomRoom.Name]["Stairs"]
local prevHadStairs = room.info[PrevRoom.Name]["Stairs"]
if (PrevRoom.Name == randomRoom.Name)
or (direction and direction == room.lastTurnDirection)
or (hasStairs and prevHadStairs)
then
return room.GetRandom(PrevRoom)
else
if direction then
room.lastTurnDirection = direction
end
return randomRoom
end
end
function room.Generate(PrevRoom, number)
local randomRoom = room.GetRandom(PrevRoom)
local newRoom = randomRoom:Clone()
newRoom.PrimaryPart = newRoom.Entrance
newRoom:PivotTo (PrevRoom.Exit.CFrame)
newRoom.Entrance.Transparency = 1
newRoom.Exit.Transparency = 1
--issue possibly w/in here vv
local requiresKey = false
local locations = furniture.FurnishRoom(newRoom)
if locations then
if room.random:NextInteger(1, 3) == 3 then
local random = room.random:NextInteger(1, #locations)
local randomLocation = locations[random]
requiresKey = true
item.New(randomLocation, "Key")
end
end
local newDoor = door.New(newRoom, number, requiresKey)
newRoom.Parent = workspace.GeneratedRooms
return newRoom
end
return room
--issue possibly w/in here ^^
Item script
This script also may be related to the issue but I’m only like 5% sure it is
local item = {}
function item.Interact(player, prompt, template, itemName)
if player.Character then
if itemName == "Key" then
local tool = workspace.Items.Key:Clone()
tool.Parent = player.Character
tool.Handle.KeyJingle:Play()
end
prompt.Enabled = false
template:Destroy()
end
end
function item.New(location, itemName)
local itemObject = workspace.Items:FindFirstChild(itemName)
if itemObject then
local itemHandle = itemObject.Handle:Clone()
itemHandle.Position = location.WorldPosition
local weld = Instance.new("WeldConstraint")
weld.Part0 = itemHandle
weld.Part1 = location.Parent
weld.Parent = itemHandle
itemHandle.Parent = location
local prompt = Instance.new("ProximityPrompt")
prompt.ActionText = ""
prompt.MaxActivationDistance = 5
prompt.Parent = location
prompt.Style = "Custom" --added
prompt.Triggered:Connect(function(player)
item.Interact(player, prompt, itemHandle, itemName)
end)
end
end
return item
UPDATE
I’ve added prints to the script & it seems the ‘print(“DESK ITEM SPAWN”)’ does not seem to be printing in the output. How do I fix? I’m a noob at printing so I’m not exactly sure what I’m doing
Updated Furniture Script W/ Prints
function furniture.New (template, roomModel)
local furnitureModel = workspace.Furniture:FindFirstChild(template.Name)
if furnitureModel then
furnitureModel = furnitureModel:Clone()
furnitureModel:PivotTo(template.CFrame)
local itemLocations = {}
print("DESK")
if furnitureModel:FindFirstChild("CDesk") then
for i, desk in ipairs(furnitureModel.Desk:GetChildren()) do
table.insert(itemLocations, desk.Location)
print("DESK ITEM SPAWN")
end
end
print("DRAWERS")
if furnitureModel:FindFirstChild("Drawers") then
for i, drawer in ipairs(furnitureModel.Drawers:GetChildren()) do
table.insert(itemLocations, drawer.Location)
drawer:SetAttribute("Open", false)
drawer:SetAttribute("Moving", false)
local prompt = Instance.new("ProximityPrompt")
prompt.ActionText = ""
prompt.MaxActivationDistance = 5
prompt.Parent = drawer.Toggle
prompt.Style = "Custom" --added
prompt.Triggered:Connect(function()
if drawer:GetAttribute("Moving") == false then
furniture.OpenDrawer(drawer)
end
end)
end
end
Output Screenshot
Please let me know if I can give you any more information!