Help with Event spawning

Hello, I am having trouble with figuring out how I can spawn an event in a part on maps, here is what the maps look like in the explorer:

Screenshot_128

Here is half of the script for the map voting, I don’t know how I would somehow make another variable with the maps, I want to know how I could make the script look for a specific part called “EventSpawn” inside the maps models and then teleport the event to that part (The event is a model and is a humanoid).

How could I do this?

Here is half of the script for map votes, they are functions:

local function highestVote()
	local map, highest = nil, 0
	
	for i, v in pairs(votes) do
		if v >= highest then
			map = i
			highest = v
		end
	end
	
	return map
end

local function voteMaps()
	votes = {}
	voted = {}
	
	local maps = {}
	local mapsSelection = ReplicatedStorage.Maps:GetChildren()
	
	repeat
		local map = mapsSelection[math.random(1, #mapsSelection)]
		if not table.find(maps, map.Name) then
			table.insert(maps, map.Name)
			votes[map.Name] = 0
		end
	until #maps == 2
	
	ReplicatedStorage.RemoteEvents.Vote:FireAllClients(maps)
	
	task.wait(10)
	
	local selectedMap = ReplicatedStorage.Maps[highestVote()]:Clone()
	selectedMap.Parent = workspace.CurrentMap
	
	ReplicatedStorage.RemoteEvents.Vote:FireAllClients()
end

ReplicatedStorage.RemoteEvents.Vote.OnServerEvent:Connect(function(player, map)
	if votes[map] and not table.find(voted, player.Name) then
		table.insert(voted, player.Name)
		votes[map] += 1
		ReplicatedStorage.RemoteEvents.Update:FireAllClients(votes)
	end
end)
2 Likes

Could use PivotTo method on the HumanoidRootPart of the Model to bring it to the event.

Yes but I want to know how I could somehow script the functionality of teleporting the event to the part, I used functions for the maps and the variables are only available to those functions, I dont know what I could do to make the script look for the part

You can loop through the descendants of the map via a for pairs loop on :GetDescendants() of the map model IF the EventSpawn part is going to be anywhere else in the model but the root of it. This solution would allow you to get the EventSpawn part no matter where the part is located inside of the map model However for simplicity you could just keep the EventSpawn part inside the root of the map model and get it via :FindFirstChild(), that’s up to you.

As for teleporting, like javi said you can just use :PivotTo() to teleport your event model to the EventSpawn part. :PivotTo() function takes CFrame as its parameter, so what you can do is get the CFrame of the EventSpawn part and pass it to the :PivotTo(). Here’s an example code:

local EventSpawnPart = mapmodel:FindFirstChild("EventSpawn"); -- assuming that the 'EventSpawn' part is inside of the map model root, we can just call findfirstchild and get the part.
local EventModel = eventmodelhere

if (EventSpawnPart == nil) then
	error("EventSpawnPart was not found inside map model!"); -- incase 'EventSpawn' is nil, throw an error
end

local TargetCFrame = EventSpawnPart.CFrame; -- assigning the CFrame of the 'EventSpawn' part to the TargetCFrame variable
EventModel:PivotTo(TargetCFrame); -- using :PivotTo() on the event model to teleport the entire model to 'EventSpawn' part CFrame, we are using the TargetCFrame as the parameter for PivotTo()

IF you want the script to search the map model for the EventSpawn, no matter the location, here’s the code:

local mapModel = mapmodelhere;
local eventSpawn = nil; -- pre-defining variable where we will keep the EventSpawn part

-- Looping through all descendants in the map model until we find the 'EventSpawn'
for _,v in pairs (mapModel:GetDescendants()) do
	if (v.Name == "EventSpawn") then
		print("found event spawnpoint");
		eventSpawn = v; -- once found, set the eventSpawn variable to the found instance
		break;
	end
end

if (eventSpawn == nil) then
	error("EventSpawn not found in map model"); -- incase 'EventSpawn' was not found inside the model, throw an error
end

-- Rest of the code you want here

I hope this is of help.

Good luck!

I have tried this, but whenever it selects the Event, then the game comes up with an error saying: ServerScriptService.Scripts.ServerScript:111: attempt to call missing method ‘GetDescendants’ of table.

Also I’m still learning some of this stuff but I don’t know if the mapmodel variable might not work because I have already mentioned it inside of a function for the map voting system.

Thank you for the help though!

This error seems to be from an attempt to use :GetDescendants() on a regular table. The :GetDescendants() function only works on Instances (like Models, Parts, etc etc…) and nothing else. I believe that your error is that you are calling :GetDescendants() on the wrong variable that isn’t referring to an instance but, as it says. some table. Double check what you are calling that function on.

If you are calling :GetDescendants() on the mapmodel variable and you’re getting the missing method of table error, that’s a sign that the mapmodel variable isn’t what it is supposed to be. Looking at your code again, the variable selectedMap would be the mapmodel that you would run the :GetDescendants() loop on.

It printed out Event spawnpoint found, but then an error comes up about the PivotTo function: Unable to cast Instance to CoordinateFrame

It’s starting to work now but it’s just the PivotTo function, how would I write the function, here is what I did:

selectedEvent:PivotTo(eventSpawn)

What does your event model in question consist of, just a model with a humanoid instance in it?

Well, it’s just an R6 rig with a block shaped head

After further testing, the error seems to be caused by the attempt to use a value different than a CFrame (CoordinateFrame) as the parameter to :PivotTo(). Make sure you’re providing the right value to the PivotTo function.

I’m confused, do you mean like using CFrame inside of the function?

CFrame is a property of Instances like parts and such which holds the values of their 3D position and orientation (basically, position and orientation properties combined). You can see that property by simply indexing it.

Let’s say you have a part.

Each part in your game has it’s own CFrame value, which determines their position and how they are oriented. (their rotation)

You can do part.CFrame to see the value of CFrame.

You can see more on CFrames here: CFrame

What :PivotTo() needs as the parameter is that CFrame value so it can set the CFrame value of the model you’re running the function on to the target CFrame (in this case, the CFrame of the ‘EventSpawn’ part you’d like to teleport your event model to).

I have attached a screenshot for reference, where I have printed the CFrame property value of a part in Workspace.

I have also attached a short clip to demonstrate how PivotTo works. Hope it helps you understand.

Thank you so much for your help! I have tried this and now errors popped up!