How do you teleport characters in Roblox without leaving residue? - roblox

How do you teleport characters in Roblox without causing the HumanoidRootPart to be out of sync with the character? I have found that moving just the HumanoidRootPart will cause the rest of the character to snap back in place where you want them to be, but to the server, it is still in place before the teleport. Is there a better method?

Yes, you can use model:MoveTo() to move models (characters are models).
For example, a simple teleporter:
local debounce = false
-- the teleporter is just a part with the script it's child
script.Parent.Touched:Connect(function (hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and not debounce then
debounce = true
local char = player.Character
local rootPart = char:WaitForChild("HumanoidRootPart")
char:MoveTo(rootPart.Position + Vector3.new(0, 10, 0))
wait(.5)
debounce = false
end
end)

Related

Im making a PropHunt game in roblox but the whenever i morph the player character into a new form the UI keeps on being called

I have this project, im creating a prophunt game, however im in a little road block that i can't understand.
Here is how my code works, I have a local script which tells me which team the player is in, since the player does not have a team yet they are always assumed as neutral, then the local script is called whenever the players are added, which a GUI appears, allowing for them to pick the team.
Heres what i did for the Local script GUI, I'm just learning this for now so not really the best way i could have coded it right now, but it does simulate what i want to happen.
Player.PlayerAdded:Connect(function(player)
if player.Team == TeamService.Blue then
Frame.Visible = false
else
Frame.Visible = true
end
end)
Hiders.MouseButton1Click:Connect(function(player)
local result = ReplicatedStorage.RemoteFunction:InvokeServer(TeamHiders)
print(result)
if result == true then
Frame.Visible = false
else
Frame.Visible = false
end
end)
Here's what i did for the server script to assign the players a team, I'm sorry I'm still fairly new to roblox so i don't know if my approach is correct im just going with what closely gets me to the output.
game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function (player, Team)
if Team == teamService.Blue then
player.Team = Team
print(ChoosenTeam)
return "player Team is blue"
end
if Team == teamService.Red then
player.Team = Team
print(player.Team)
return "player Team is red"
end
end
And here is my script for the Morph, so i got this click detector which should tell the game that whenever its clicked transform the player into whatever form this is scripted on.
local model = script.Parent.Parent
script.Parent.MouseClick:Connect(function(player)
if player.Team == teamservice.Blue then
local oldCharacter = player.Character
local newCharacter = model:Clone()
newCharacter.HumanoidRootPart.Anchored = false
newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)
player.Character = newCharacter
newCharacter.Parent = workspace
end
end)
The players under blue side should only encounter this once since the UI should only appear when they do not have a team or are loaded in the game, but what happens is the blue players whenever they morph into a block they encounter the GUI prompt again.
I was expecting by limiting the player teams to nil it would only show the UI, and if the player has a team already then the UI would not show itself, the same goes to when the player decides to morph the player UI should not be visible.
So turns out i can just turn off spawn on reset.

Part moving towards screen?

The part is supposed to move to the mouse's position and it worked almost fine but the thing is it moves towards my screen...
Local script:
local moveevent = script:WaitForChild("MoveEvent")
script.Parent.Parent.Equipped:Connect(function(mouse)
mouse.Move:Connect(function()
moveevent:FireServer(mouse.Hit.p)
end)
end)
Script:
local moveevent = script.Parent:WaitForChild("MoveEvent")
moveevent.OnServerEvent:Connect(function(plr, mousepos)
workspace:WaitForChild("Spawnpart").Position = mousepos
end)
In local script:
mouse.TargetFilter = workspace:WaitForChild("Spawnpart")
What's happening is that your mouse it hitting the part instead of what's behind it. Read more

How can you load the player character when they join?

I want to make a Roblox game with multiple scenes that you can load your character into.
The problem with this is that I cannot find any way to do this. I’ve done a reasonable amount of research online, but all I get are guides on how to load a character once using a plugin, rather than getting the person who joined’s character and loading it into a certain position.
How to get the person who joined’s character and load it into a certain position?
To create somebody's character, call Players:CreateHumanoidModelFromUserId with the Player's UserId. Then you can move it by calling PivotTo on their HumanoidRootPart.
Note: do not use Model.PrimaryPart to grab the HumanoidRootPart. On R6 characters, this is actually the character's Head. You're better off calling FindFirstChild("HumanoidRootPart") (or WaitForChild).
You'll notice that this positions the character from their torso's center, and not from their feet. So you'll need to offset that vertically by 3 studs if the character is not Rthro (is of fixed size, like R6). If the character is Rthro (is R15, and your game has scaling enabled), then you'll need to call GetExtentsSize on the character Model.
So, something like:
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local function loadStage(player: Player)
-- ...
local character = Players:CreateHumanoidModelFromUserId(player.UserId)
local rootPart = character:FindFirstChild("HumanoidRootPart")
-- assuming R6 this will be fine
rootPart:PivotTo(CFrame.new(myDesiredPosition + Vector3.yAxis * 3))
rootPart.Parent = Workspace
-- ...
end
Players.PlayerAdded:Connect(function(player)
loadStage(player)
end)

How to make object following player smooth

I use MoveTo for following the position and change the CFrame for rotation but it looks supper jaggy. I think the problem is that the code is on the server-side and not on the client-side since I want other players to be able to see the object following the player
Try using weldConstrain. You can create it by code and connect 2 parts to it. The parts are going to stay at the same relative position from each other. Here is a link for the API reference about the subject: https://developer.roblox.com/en-us/api-reference/class/WeldConstraint
Here is an example code that adds a part at the top of a player's head when it joins the game:
local Players = game:GetService('Players') -- get's the players service
local function OnPlayerAdded(player)
local PlayerCharacter = player.Character
-- waits until the player's character loads
while PlayerCharacter == nil do
wait(1)
PlayerCharacter = player.Character
end
-- Get's the player's model in workspace
local PlayerModel = workspace:WaitForChild(PlayerCharacter.Name)
-- Get's the player's head inside it's model
local PlayerHead = PlayerModel:WaitForChild('Head')
-- Write the path to the part you want to follow the player, bellow
local FollowPartOriginal = workspace:WaitForChild('Part')
-- If you want to clone the part, so more players can be followed by it,
-- don't delete the line below, if you want only 1 part, delete the line below.
local FollowPart = FollowPartOriginal:Clone()
-- set's the part position on top of the player's head, with an offset
FollowPart.Position = PlayerHead.CFrame.p + Vector3.new(0,2,0)
FollowPart.Parent = workspace
-- creates a weldconstrain
local WeldConstraint = Instance.new('WeldConstraint')
-- connect's the weld to the player's head and the other part
WeldConstraint.Part0 = PlayerHead
WeldConstraint.Part1 = FollowPart
WeldConstraint.Parent = PlayerHead
end
Players.PlayerAdded:Connect(OnPlayerAdded)
Place this code in a script(not a local script).
If you use the function separately, don't forget to put the player parameter. The parameter needs to contain the player Instance(ex: game.Players.PlayerName)
To change the part position, use the offset in this line of the code:
FollowPart.Position = PlayerHead.CFrame.p + Vector3.new(write offset here)
If you want the part the stop following the player, make a script to delete the part, or the weld constrain(child of the player's head)

Roblox - while testing trusses work when i drag them in, but when i use script to set all the stuff, then i cant climb it

I am using a script to set cancollide = true and transparency = 0, but i cant climb the truss. but when I'm already in testing mode (in studio) and i drag in the same truss, i can climb it, and i am looking at properties, its same, both anchored is true, they are touching same parts, i dont know why this is happening. Please help thanks :)by the way, i am making tycoon and this is script i am using:
wait(1)
amount = 0 -- cost of model
owner = script.Parent.Parent.Owner
local stun = false
pcall(script.Parent.Head.Touched:connect(function(hit)
if hit.Parent ~= nil then
player = game.Players:findFirstChild(hit.Parent.Name)
if not stun and player ~= nil then
if player.Name == owner.Value then
if player:findFirstChild("leaderstats") ~= nil then
stats = player:findFirstChild("leaderstats")
if stats.Money.Value >= amount then
stun = true
stats.Money.Value = stats.Money.Value - amount
script.Parent.ladder.CanCollide = true
script.Parent.ladder.Transparency = (0)
script.Parent.Head:Remove()
wait(1)
stun = false
end
end
end
end
end
end))
dont worry about other stuff, it works, its just this part that matters now:
script.Parent.ladder.CanCollide = true
script.Parent.ladder.Transparency = (0)
script.Parent.Head:Remove()
Please help :( this is the problem with the ladder using script not working and same dragged in from toolbox truss working, iv done this with many trusses and ladders and same result :(
So, a error I noticed in your code, is that you erase your Character's Head, which would kill your character, or possibly throw a error if the .Touched event fires two times.
There are many efficency errors in this script, such as .Transparency = (0), or doing script.Parent.ladder instead of using variables, but it really is no problem most of the times. Something you could try, is to use Instance.new() to create the ladder.If this game is FilteringEnabled on (Expermiental mode off), please note your script won't work as intended if it is a LocalScript, it will only work well on Server-Side (aka, a normal Script instead of a LocalScript)