Ad
Log in to vote
0

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

Asked by 22 days ago

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.

0
Any further questions, you can ask me Cubispaghettis 1 — 22d

1 answer

Log in to vote
1
Answered by 22 days ago
Edited 22 days ago

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)
0
It will not teleport me for some reason. I put that inside of ServerScriptService. Cubispaghettis 1 — 21d
0
I meant, it does but doesnt take my points currency away. Cubispaghettis 1 — 21d
0
OH I DIDNT PUT IT INSIDE OF THE TEXT BUTTON I got it thanks !!! Cubispaghettis 1 — 21d
Ad

Answer this question