Hello,
I'm making a script in which when you press the button, the camera will move from the Part's CFrame and be normal again. I have no problem with that. But when I try and teleport the player to one of the 8 spawns, the player just stays where they are, and an error pops up.
OUTPUT : LocalScript:9: attempt to index local 'spawnpart' (a nil value)
here's the script:
local players = game:GetService("Players")
local player = players.LocalPlayer
local button = script.Parent
local Camera = game.Workspace.CurrentCamera
button.MouseButton1Click:Connect(function()
local spawnpart = game.workspace:FindFirstChild("Spawn"..math.random(1,8))
local humanoidRootPart = player.Character.HumanoidRootPart
humanoidRootPart.CFrame = CFrame.new(spawnpart.Position.X, spawnpart.Position.Y + 5, spawnpart.Position.Z) -- the issue is here.
end)
button.MouseButton1Click:Connect(function()
Camera.CameraType = 'Custom'
Camera.CameraSubject = player.Character
end)
The button is inside a frame, which is inside a ScreenGui. The Script is a local script, and it's inside the button.
I don't understand what's wrong though. I've defined the parts that I want them to be teleported to by doing local spawnpart = game.workspace:FindFirstChild("Spawn"..math.random(1,8)) , so it's one of the spawns between 1-8
But I still don't understand where I have gone wrong, please can someone help me?
Hierarchy : Spawns -- https://gyazo.com/3c4981057ba8541d8d5c5d7b4aabebbf

4 comments
if the hyperlink doesn't work, just copy and paste into address bar
Are the parts anchored? I had a similar problem yesterday and it was purely due to the fact they just fell down to the skybox and got destroyed
I guess the parts un anchored themselves. i had them anchored like a few weeks ago, and when I made a proper map for the game, they seemed to have un anchored themselves!
Haha, I know how it feels lol