-
What do you want to achieve?
I want to make my sword fight game(by me and original) and I have a bug with it that I can’t fix. -
What is the issue?
The part on the script that is bugging is where I want to teleport the players to the map
this script may look vaguely familiar to alvinbloxes sword fight game, but I promise I planned this myself I am just coding it on my own. I am just using some things that alvinblox did in his tutorial. like using the status value for displaying messages.
here’s the code and look on the comments to see where is the error.
-- define variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local StarterGui = game:GetService("StarterGui")
local Status = ReplicatedStorage:WaitForChild("StatusValue")
local MapsFolder = ServerStorage:WaitForChild("Maps")
-- first we need to create the loop
while true do
wait(5) -- wait so then the script dosen't crash
Status.Value = "Waiting for enough players..."
repeat wait() until game.Players.NumPlayers >= 1
Status.Value = "Intermission"
-- Add all players in table in put them in it
local Plrs = {}
for i,player in pairs(game.Players:GetChildren()) do
if player then
table.insert(Plrs,i)
end
end
-- choose a random map and clone it and put it in the workspace.
local AvaibleMaps = MapsFolder:GetChildren()
local ChosenMap = AvaibleMaps[math.random(1,#AvaibleMaps)]
Status.Value = ChosenMap.Name.."chosen"
ChosenMapClone = ChosenMap:Clone()
ChosenMapClone.Parent = game.Workspace
-- teleport the players to the map
-- first make a table with all of the avaible spawn points.
local SpawnPositions = ChosenMap:FindFirstChild("SpawnPoints")
local SpawnPoints = SpawnPositions:GetChildren()
-- start spawning the players
for i,player in pairs(Plrs) do
if player then
character = player.Character -- it is erroring right here.
if character then
character:FindFirstChild("HumanoidRootPart").CFrame = SpawnPoints[1].CFrame
table.remove(SpawnPoints,1)
end
else
-- there is no avaible character so remove them.
if not character then
table.remove(Plrs,i)
print("character no found")
end
end
end
end
thank you for reading!