So, Im trying to make a GUI button that when u click it u reset and get teleport back to ur old location/cframe
ive tried to make a part that has the cframe and it TPs u to the part (doesnt work)
ive tried to make a variable that stores the old cframe then TPs u to it (doesnt work)
One problem is your redefining the variable (local oldCFrame) each time you click. So, everytime you click it, it deletes your old CFrame and sets it to your current CFrame.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local oldCFrame = humanoidRootPart.CFrame
script.Parent.MouseButton1Click:Connect(function()
--This teleports the player
humanoidRootPart.CFrame = oldCFrame
--this sets the old position of the humanoidRootPart to the oldCFrame, thus, you can position it correctly
oldCFrame = humanoidRootPart.CFrame
end)
TIP: It might be best, though, to use remotes to position the player rather than have it all done by the client.
Instead of updating the CFrame relying on wait(.5) which will not ensure the character has finished loading(and therefore a PrimaryPart is set) rely on an event-based solution:
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local oldCFrame = CFrame.new(0, 0, 0) --default spawn
script.Parent.MouseButton1Click:Connect(function()
oldCFrame = Player.Character.HumanoidRootPart.CFrame
task.wait(.3)
Player.Character.Humanoid.Health = 0
end)
Player.CharacterAdded:Connect(function(char)
repeat task.wait() until char.PrimaryPart
char:SetPrimaryPartCFrame(oldCFrame)
end)
It’s because this is a localscript. Whenever the player clicks the button, fire a remote event which you pick up in the server with the arguments as Player and CFrame, then TP the player. Don’t teleport a player locally.