I created a script to teleport players once they click a gui button,
It does teleport players, but it damages and kills them which is a problem.
Here is the script I used to teleport players, local script in the button :
disabled = false
script.Parent.MouseButton1Click:connect(function()
if disabled == false then
disabled = true
script.Parent.BackgroundColor3 = Color3.new(217, 0, 0)
game.ReplicatedStorage.RemoteFunction:InvokeServer()--here
wait(3)
script.Parent.BackgroundColor3 = Color3.new(0, 154, 0)
disabled = false
end
end)
and a script in server script service :
local remote = game.ReplicatedStorage.RemoteFunction
local part = workspace.TeleportPart2
remote.OnServerInvoke = function(player)
local humanoid = player.Character.Humanoid
if humanoid.Parent:FindFirstChild("Torso") then
humanoid.Parent.Torso.CFrame = CFrame.new(part.Position + Vector3.new(0,3,0))
elseif humanoid.Parent:FindFirstChild("UpperTorso") then
humanoid.Parent.UpperTorso.CFrame = CFrame.new(part.Position + Vector3.new(0,3,0))
end
end
for an unknown reason the players get damaged too, there are no other scripts related to that part.
If there is a better way to teleport players upon a gui button being clicked then do tell me .
Thanks.
i’m not very familiar with Server Invoke, the second script is modified and isn’t mine
And kills? Weird. You’re setting the CFrame so death shouldn’t be an issue. Only ever had that problem when setting the position of the Torso or Head. Maybe you have another underlying issue?
It’s worth noting, by the way, that you don’t need that if statement to check whether the rig is R6 or R15. Not only is that not the right way to check the active rig (you’d use Humanoid.RigType), but you should be using the HumanoidRootPart to teleport characters. Both rig types have one and they are made the same way.
One other thing. As you are using a RemoteFunction, you must return something. Don’t know if you already are and the code sample was truncated, but if not, add a return statement before the last end (the default return, essentially). If the server does not return a value to the client, the thread will be permanently hung. Anything below the InvokeServer will not run.
thanks for the tip sentient, but what would I have to change so that that height doesn’t damage players?
I’ve tried changing fallen parts destroy height, but that was of no use.
You do not need to explicitly return a remote function for it to work properly. When the function ends on its own, the server sends an empty response, which is the same as a normal Lua function with no return value.
Try moving the humanoid root part instead of the upper torso / torso. Maybe you’re moving the torso away from the head momentarily, and that’s killing them?
I think the problem is that you are setting the position of the torso, not the humanoidrootpart. Now, I know that this has been said, but I think the point is, that, if you remove the head from a part of the body, the player dies. And I think that the script is causing that
As others said, the issue is setting the CFrame of Torso & UpperTorso. The PrimaryPart property of Characters is the HumanoidRootPart, meaning if you set the CFrame of this part, the rest of the Character follows. UpperTorso and Torso are not natural PrimaryParts, meaning you’d simply be killing the Player when offsetting these objects’ CFrames from the Character. Here is something like what your Server Script should look like:
local remote = game.ReplicatedStorage.RemoteFunction
local part = workspace.TeleportPart2
remote.OnServerInvoke = function(player)
local humanoid = player.Character.Humanoid
if humanoid.Parent:FindFirstChild("Torso") then
humanoid.Parent.HumanoidRootPart.CFrame = part.CFrame + Vector3.new(0, 3, 0))
end
end
``
What do you mean? RemoteFunctions do not send an empty response when they are complete, they require an explicit return statement, unless that’s been changed and I simply never knew.
Users have encountered crash issues and frozen threads in the past (hung threads due to no response) because they did not add a return statement to the remote. Scripts expect return values from remotes. The only time this works is a callback function off a RemoteFunction where you return things except a default value.
A RemoteFunction invocation expects a response. A regular callback function does not and will return an empty (nil) response by default after it’s execution.
Correct me if I’m wrong? I’m sure this behaviour hasn’t changed though.
Are you able to provide a video of the issue occurring? I find this to be a very strange problem. Your code looks fine so there shouldn’t be any death problems which is why I’m banking my money on there being an underlying issue.
Although I have marked the appropriate fix to my problem, as to not kill players once they teleport. For some strange reason the code in the local script does not work (parented to the gui button)
disabled = false
script.Parent.MouseButton1Click:connect(function()
if disabled == false then
disabled = true
script.Parent.BackgroundColor3 = Color3.new(217, 0, 0)
game.ReplicatedStorage.RemoteFunction:InvokeServer()--here
wait(1.5)
script.Parent.BackgroundColor3 = Color3.new(0, 154, 0)
disabled = false
end
end)
Thats because your using a Remote Function and need to return.
local remote = game.ReplicatedStorage.RemoteFunction
local part = workspace.TeleportPart2
remote.OnServerInvoke = function(player)
local humanoid = player.Character.Humanoid
if humanoid.Parent:FindFirstChild("HumanoidRootPart") then
humanoid.Parent.HumanoidRootPart.CFrame = part.CFrame + Vector3.new(0, 3, 0))
return true
end
end
Here’s proof that a remote function returns on its own.
-- Script
local Remote = game.ReplicatedStorage.DoSomething
function Remote.OnServerInvoke(player)
print("Did something.")
end
-- LocalScript
local Remote = game.ReplicatedStorage.DoSomething
wait(3)
print("Invoking server function.")
Remote:InvokeServer()
print("Server function returned.")
Oh? Did they change the behaviour then? I very clearly remember that threads would hang if an explicit return value was not made. Behaviour when defining a variable to the return of a RemoteFunction isn’t there though, but maybe it’d be the same.
TIL. Does that also mean I could safely start using InvokeClient and not worry about clients overwriting OnClientInvoke, or create a timeout system if clients do not respond?
Thank you for the citation and test. I could’ve tested that myself though, but, not often you get a reply with an actual test and citation to disprove something.
Ive tested in a few circumstances and yes it has always returned nil when a return isn’t made, they just haven’t updated the Remote Function Roblox page.