Error with this code!

Hello there,

I am trying to make a GUI Button that once you press it you get teleported to a specific location.

I am using a Server Script inside an ImageButton.

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Transition = ReplicatedStorage:WaitForChild("Transition") 

script.Parent.MouseButton1Click:Connect(function(player)
	print(player) --This prints nil.
		local character = player.Character or player.CharacterAdded:Wait()
		local hrp = character:FindFirstChild("HumanoidRootPart")

		if hrp then
			Transition:FireClient(player)
			wait(.5)
			local hrp = game.Workspace:FindFirstChild(player.Name):FindFirstChild("HumanoidRootPart")
			hrp.CFrame = game.Workspace.GroupA.DoorGroup.teleporthere.CFrame + game.Workspace.GroupA.DoorGroup.teleporthere.CFrame.LookVector* 4

		end
end)

My Issue
The player function is nil so the script doesn’t work. Is there a way to fix that?

If you have any idea please let me know below!

Thanks,
@Legend_Pavlos

2 Likes

Use a local script, server scripts don’t work for Guis

As @boterflic5 said use local script.


Other stuff to improve your script:

  • Use task.wait instead of wait
  • MouseButton1Up instead of MouseButton1Click
  • Get player like this local player = game.Players.LocalPlayer instead of getting it in the function
  • Use WaitForChild when getting the link to an instance
  • Don’t check for hrp, do local hrp = character:WaitForChild("HumanoidRootPart

MouseButton1Click does not return a Player object, and teleporting a player through a Button should be handled both by the client and the server in two different scripts. Also, Scripts are not suited for GUIs, as their performance depends on the server’s current workload (This is an ancient practice that should be avoided).

Place one RemoteEvent under ReplicatedStorage, and have one LocalScript under the GUI button, and a Script under ServerScriptService. Both Scripts should local remote = ReplicatedStorage:WaitForChild("RemoteEvent"). In a LocalScript, listen for the button’s MouseButton1Click, and when the button is pressed, fire the RemoteEvent (remote:FireServer())

In the server Script, listen to the RemoteEvent’s OnServerEvent event, and you can access the Player object through there. Then, teleport that player:

LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Transition = ReplicatedStorage:WaitForChild("Transition")

local Remote = ReplicatedStorage:WaitForChild("RemoteEvent")

script.Parent.MouseButton1Click:Connect(function()
	Remote:FireServer()
end)

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("RemoteEvent")

local TeleportLocation = workspace.GroupA.DoorGroup.teleporthere

Remote.OnServerEvent:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local hrp = character:FindFirstChild("HumanoidRootPart")

	if hrp then
		Transition:FireClient(player)
		task.wait(.5)
		local hrp = workspace:FindFirstChild(player.Name):FindFirstChild("HumanoidRootPart")
		hrp.CFrame = TeleportLocation.CFrame + TeleportLocation.CFrame.LookVector * 4
	end
end)

MouseButton1Click also doesn’t have any parameters, so it will always return nil

have player be outside the event as

local player = game.Players.LocalPlayer

Thank you so much for pointing me out the issues! I fixed the code and now it’s working.

1 Like

When it’s player using MouseButton1Click, it says a or any or something like that.

Change it to a LocalScript.

Add a line which is:

local player = game:GetService("Players").LocalPlayer

After local Transition = ReplicatedStorage:WaitForChild("Transition") .

This is HOW your Script SHOULD Be:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Transition = ReplicatedStorage:WaitForChild("Transition")
local player = game:GetService("Players").LocalPlayer -- If LocalScript.

script.Parent.MouseButton1Click:Connect(function()
	print(player) 
	local character = player.Character or player.CharacterAdded:Wait()
	local hrp = character:FindFirstChild("HumanoidRootPart")

	if hrp then
		Transition:FireClient(player)
		wait(.5)
		local hrp = game.Workspace:FindFirstChild(player.Name):FindFirstChild("HumanoidRootPart")
		hrp.CFrame = game.Workspace.GroupA.DoorGroup.teleporthere.CFrame + game.Workspace.GroupA.DoorGroup.teleporthere.CFrame.LookVector* 4
	end
end)

If you’re using a Server Script, try doing:

local player = game:GetService("Players"):FindFirstChildOfClass:("Player") -- Player = LocalPlayer

If it’s a ServerScript, do this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Transition = ReplicatedStorage:WaitForChild("Transition") 
local player = game:GetService("Players"):FindFirstChildOfClass:("Player") -- If Server Script.

script.Parent.MouseButton1Click:Connect(function()
	print(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local hrp = character:FindFirstChild("HumanoidRootPart")

	if hrp then
		Transition:FireClient(player)
		wait(.5)
		local hrp = game.Workspace:FindFirstChild(player.Name):FindFirstChild("HumanoidRootPart")
		hrp.CFrame = game.Workspace.GroupA.DoorGroup.teleporthere.CFrame + game.Workspace.GroupA.DoorGroup.teleporthere.CFrame.LookVector* 4
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.