Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I allow a player to teleport to a certain point when pressing a TextButton? (Solved)

Asked by
2Loos 168
3 years ago
Edited 3 years ago

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?

3 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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

.

Ad
Log in to vote
0
Answered by
2Loos 168
3 years ago

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!

0
You are teleporting the player on the client, don't do that. R_alatch 394 — 3y
Log in to vote
0
Answered by
R_alatch 394 Moderation Voter
3 years ago
Edited 3 years ago

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)

Answer this question