Not understanding what's going wrong

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
2 Likes

It would appear you are storing your positions wrong, you need to store them as Vector3 values as opposed to individual number values.

When trying to set the “v.Handle.Position = SelectedPosition”, it will only read the first number in the sequence thus erroring.

local RandomPosOfficeKey = {
   Pos1 = Vector3.new(0.62, 239.73, -52.12);
   Pos2 = Vector3.new(50.55, 244.39, -89.61);
   Pos3 = Vector3.new(115.56, 240.557, -31.24);
   Pos4 = Vector3.new(121.131, 220.422, -76.109)
}

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.

local RandomPosOfficeKey = {
   Vector3.new(0.62, 239.73, -52.12);
   Vector3.new(50.55, 244.39, -89.61);
   Vector3.new(115.56, 240.557, -31.24);
   Vector3.new(121.131, 220.422, -76.109)
}
1 Like

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 am new to collection service btw, and my keys are in a folder called Tagged and a folder called keys in it. In server storage

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.

Good Luck!

2 Likes

Still having no idea what to do, tried with breakpoints, not getting anything