Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make it so you pay leaderstats currency to teleport to a part via GUI?

Hello, I have a scripting question. So I have it so there is a Text Button inside of a frame, inside of a Screen Gui, inside of Starter Gui. So basically I want the textbutton to teleport you to a specific part, but I also want it to make it so it checks if you have a specific amount of points and take that amount away from you, like basically paying for it. I want to make it so when you click that text button, you are required to pay 1000 points (from leaderstats) and if you have it, they take those 1000 points away (like paying in a shop) and in exchange, teleport you to a specific part/position. I am aware that the text button has to be the same name as the name of the part for it to teleport to that part, by the way. I ALSO HAVE A LEADERSTAT SET. THE SCRIPT WORKS AND TELEPORTS ME BUT I also want to make it so you have to pay those 1000 points. Here is my code so far to make it so the text button teleports you to a part:

wait(1)
player = game.Players.LocalPlayer
button = script.Parent
local debounce = false

function teleport()
	if not debounce then
		debounce = true
		LowerTorso = player.Character.LowerTorso
		LowerTorso.CFrame = game.Workspace.TeleportPoints.ToolsTP.CFrame
	end
end

button.MouseButton1Click:Connect(teleport)
while true do wait()
	debounce = false
	wait(1)
end

Edit the code or rewrite to make it so it teleports you to that part by paying with points.. Also, "TeleportPoints" is the name of the folder in Workspace which leads you to the name of the part, ToolsTP.

1 comment

0

Any further questions, you can ask me

1 answer

✓ Accepted answer

I recommend changing this script to a server script rather than using a remote event. I say this because you can still obtain the player in the server script, as the script is a descendant of the player. You also want to check/change leaderstat values on the server, as the client can manipulate and change them.

local player = script:FindFirstAncestorOfClass("Player")
local leaderstats = player:WaitForChild("leaderstats")
local button = script.Parent
local debounce = false

local cost = 1000

local function teleport()
	if not player.Character then return end
	if leaderstats.Points.Value < cost then return end

	if not debounce then 
		debounce = true

		leaderstats.Points.Value -= cost
		player.Character:PivotTo(workspace.TeleportPoints.ToolsTP.CFrame) 
		-- PivotTo will move the character and all its descendants to the given CFrame

		delay(1, function() -- No need for a while loop to set this to false. Using delay will schedule a function to be called after the given time.
			debounce = false
		end)
	end
end

button.MouseButton1Click:Connect(teleport)

3 comments

0

It will not teleport me for some reason. I put that inside of ServerScriptService.

0

I meant, it does but doesnt take my points currency away.

0

OH I DIDNT PUT IT INSIDE OF THE TEXT BUTTON I got it thanks !!!

This discussion is preserved as it was. New answers, comments, and votes are closed.