LocalScript, parented under a few frames and a screengui under StarterGui.
local SPAWNPOINTPOSITION = Vector3.new(-262, 589.8, 430) script.Parent.TextButton.MouseButton1Click:Connect(function(player) local char = player.Character char.HumanoidRootPart.CFrame = CFrame.new(SPAWNPOINTPOSITION) end)
The output:
16:20:23.564 Players.2Loos.PlayerGui.MainScreen.Spawns.Frame.Frame.LocalScript:4: attempt to index nil with 'Character' - Client - LocalScript:4
What's going on? Why isn't this working?
You must define the player in the first example you gave. An example of what your script could look like.
Updated Script:
local player = game.Players.LocalPlayer local SPAWNPOINTPOSITION = Vector3.new(-262, 589.8, 430) script.Parent.TextButton.MouseButton1Click:Connect(function(player) local char = player.Character char.HumanoidRootPart.CFrame = CFrame.new(SPAWNPOINTPOSITION) end)
Your old script was incorrect since you tried to get an undefined players character. All you had to add was a variable to define the player.
Your new script is correct since you are getting the character from the workspace:
local char = workspace[game.Players.LocalPlayer.Name]
but you could shorten the line with:
local char = game.Players.LocalPlayer.Character
.
Nevermind, solved it myself.
local SPAWNPOINTPOSITION = Vector3.new(-262, 589.8, 430) script.Parent.TextButton.MouseButton1Click:Connect(function(character) local char = game.Workspace[game.Players.LocalPlayer.Name] char.HumanoidRootPart.CFrame = CFrame.new(SPAWNPOINTPOSITION) script.Parent.Parent.Parent.Visible = false --Added this line, don't worry about it. end)
But please, feel free to improvise on this script. There might be a better way! I'd appreciate any help!
The issue with this script is that you're teleporting on the client instead of the server. To teleport on the server, you can use a RemoteEvent. Create a RemoteEvent called TeleportEvent
and put it inside ReplicatedStorage and you can use the FireServer
function to fire to the server and listen on the server with OnServerEvent
.
The use of LocalPlayer is unneeded because the player gets passed by default when calling FireServer
.
Local script:
local teleportEvent = game:GetService("ReplicatedStorage").TeleportEvent script.Parent.TextButton.MouseButton1Click:Connect(function() teleportEvent:FireServer() end)
Server script:
local teleportEvent = game:GetService("ReplicatedStorage").TeleportEvent local position = Vector3.new(-262, 589.8, 430) teleportEvent.OnServerEvent:Connect(function(player) player.Character:SetPrimaryPartCFrame(CFrame.new(position)) end)