How would I go about making a GUI where you press it it teleports you and gives you a tool?

I’m generally new to scripting. I know all the basis but I wanna get into starting having to not use video tutorials when it comes down to my coding. I want to use recourses such as the dev forum. Anyways, How would I go about making a GUI where you press it it teleports you and gives you a tool (specifically a Sword)

2 Likes
Put this "LocalScript" into a button

– Place a remote event in replicatedstorage

script.Parent.MouseButton1Down:Connect(function()
	game.ReplicatedStorage["Your RemoteEvent Name"]:FireServer() 
end)

Put this "Script" into ServerScriptService

game.ReplicatedStorage["Your RemoteEvent Name"].OnServerEvent:Connect(function(player)
	local Sword = game.ServerStorage.ClassicSword:Clone() -- get a sword and place it in ServerStorage, after naming it ClassicSword
	
	local PrimaryPart = player.Character
	PrimaryPart:MoveTo(Vector3.new(3,23,3) -- wanted position position example
	if not player:WaitForChild("Backpack"):FindFirstChild("ClassicSword") then -- this should be the sword name
		Sword.Parent = player:WaitForChild("Backpack")
	end
end)

Would I put this script into a text label or where would this script be stored?

Put it into a button ----------

You should also make it a local script or else it won’t work. And remember to get a sword and name it “ClassicSword”

Well, first you’re going to need a few things

  • a text button under a GUI,

  • a remote event in replicated storage,

  • a sword in server storage,

  • a script in server script service to handle the remote event,

  • a position you want the player to teleport to,

  • and a local script under the text button

Now, I’m not sure if you know what remote events are. If you don’t, watch a quick video on them. Anyways, let’s start scripting from the local script.

Local Script:

local button = script.Parent
local RemoteEvent = game.ReplicatedStorage.REMOTEEVENTNAME

-- first we're gonna need to know when to call the remote event. Which will be right after we click the button.

button.MouseButton1Click:Connect(function()
     RemoteEvent:FireServer()
end)

Ok now the remote event is fired, the script in server script service is going to handle the request. Obviously now we need to script that.

local RemoteEvent = game.ReplicatedStorage.REMOTEEVENTNAME

RemoteEvent.OnServerEvent:Connect(function(player)
      local sword = game.ServerStorage.Sword:Clone()
      sword.Parent = player.Backpack -- this clones the sword and inserts in into the players backpack

      player.Character:MoveTo(position) -- this will move the whole character to the position you desire.
end)

The reason we used a remote event is because we need to communicate to the server that we want a sword to be cloned into the player’s backpack. If we did this on a local script, nobody would see the sword except the player who called it.

Annnd that’s everything. Hope this helps.
Sorry this took so long. I’m on mobile copying and pasting lol
I don’t mind explaining more into the details if you’re a bit confused.

2 Likes

I learned alot from this! Thanks so much!

1 Like

Sweet! This helps alot! It works, but it there any way to stop spam? Thanks once again. This taught me alot!

1 Like

The spam give? -------------------

1 Like

yeah so basically if you have a sword you cant click it again, or u just cant click it till you respawn…

1 Like

Do you want to be able to get multiple swords at a time? (I made it so you can’t, but can fix)

Edit:

1 Like

Just remove

if not player:WaitForChild("Backpack"):FindFirstChild("ClassicSword") then -- this should be the sword name
	Sword.Parent = player:WaitForChild("Backpack")
end

and replace with

Sword.Parent = player:WaitForChild("Backpack")

That should fix the problem with Cramp’s script. (Not really a problem but a preference)

This is because “if not player:WaitForChild(“Backpack”):FindFirstChild(“ClassicSword”) then” checks if there already is a sword in the player’s backpack.

2 Likes

Perfect! This worked! Thanks a ton! :slight_smile:

Hey!

Hold on! Don’t forget. The Player’s character’s network ownership is to the respective client. Meaning! You can teleport players using the Local Script itself. You don’t need to make the server teleport unless you are teleporting between two different games.

Just make the player teleport directly using the local script.

Edit:
Regarding the Sword giver. That needs to be done on server.