For my piggy game, I basically have 4 set positions for Each key, but the thing is that, well in the script below am just trying out with just one key, is this the correct way to do it, I am using my game logic script, and another module script for the random spawning of it.
Game Logic:
local test = require(script.KeyModule)
test.RandSpawn()
local TestClone = game.ServerStorage.TagList.Keys["Office Key"]:Clone()
TestClone.Parent = game.Workspace
KeyModule
local module = {}
local CS = game:GetService("CollectionService")
local taggedKeys = CS:GetTagged("Keys")
local RandomPosMainDoorKey = {
Pos1 = -31.436, 239.97, 38.8
}
local RandomPosOfficeKey = {
Pos1 = 0.62, 239.73, -52.12;
Pos2 = 50.55, 244.39, -89.61;
Pos3 = 115.56, 240.557, -31.24;
Pos4 = 121.131, 220.422, -76.109
}
function module.RandSpawn()
for i, v in pairs(taggedKeys) do
if v == "Office Key" then
local Rand = Random.new()
local SelectedPosition = RandomPosOfficeKey[Rand:NextInteger(1,#RandomPosOfficeKey)]
v.Handle.Position = SelectedPosition
print("Office key success"..SelectedPosition)
end
end
end
return module
It also seems like you are trying to index an array as though it is a table, so we can further change the array into a table that can be parsed via numbers.
Great explanation, and I think you are right as well, but it still didn’t work, like the main thing is that the key is not even getting cloned into the workspace
I’m guessing this is a Server Side script so why not just debug the script with the Lua Debugger built into Roblox Studio and follow the code flow to see which variables are being loaded into memory e.g. location vector is being initialized. If you don’t know how the debugger works there’s plenty of tutorials on YouTube and forums.