Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a creature teleport to player position on keyinput with a localscript?

Asked by
Aeike 9
8 years ago
Edited 8 years ago

Alright. I'm back at it again with another question. I will try to be as detailed as possible.

Ok wonderful community of scriptinghelpers, I shall provide code and then explain what it's purpose is for, and then describe my issue.

This is the entirety of the code inside of the localscript I've created.

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.J then
        print("Titan Shift in action")
        a = game.ReplicatedStorage.AeikeTitan:Clone()
        a.Parent = game.Workspace
end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Here is my hiearchy.

https://gyazo.com/695496ec3fa9d3e632dd1ce6bdea40fd

Now, what I am currently aiming to do in this little project I've been working on is to make it so that when a player presses J, a custom-build creature I've put in ReplicatedStorage clones itself and the clone's parent becomes the Workspace. And from there, the creature would teleport to the players position. And then, from there, the player would teleport into the creatures neck. What happens next will be saved for another question.

INFO:

My creature's neck part is named "Neck". My creature's name in hiearchy is AeikeTitan.

How exactly do I make the teleportation of the creature to the player, and then the player to the neck of the creature, happen?

And again, using a localscript for this. Any help would be greatly appreciated!

2 answers

Log in to vote
1
Answered by
tkcmdr 341 Moderation Voter
8 years ago

Hello Aeike,

In order to achieve what you outlined, you will have to have two scripts:

  • A server script to spawn the monster, place it next to the player, and teleport the player onto it, and:

  • A client script to tell the server script when to do so (which already exists)

As well as a RemoteEvent, which we will call "SpawnCreature" and place in ReplicatedStorage. Observe:

Local Script:

local SpawnCreature = game.ReplicatedStorage:WaitForChild("SpawnCreature")

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.J then
        SpawnCreature:FireServer()
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Server Script:

local AeikeModel        = game.ReplicatedStorage:WaitForChild("AeikeTitan")
local SpawnCreature = game.ReplicatedStorage:WaitForChild("SpawnCreature")

-- Provided the player's character is alive, spawn a creature and teleport the player onto its neck.
local function OnSpawnCreatureFired(player)
    local character = player.Character

    -- Check to see if the character exists and is alive
    local characterIsAlive = (
        character and
        character:FindFirstChild("Humanoid") and
        character.Humanoid.Health > 0
    )

    if (characterIsAlive) then
        local creatureClone = AeikeModel:Clone()

        -- Finds the creature's neck, even if it isn't directly under the root model
        local creatureNeck  = creatureClone:FindFirstChild("Neck", true)

        -- We assume the Torso exists because the character is alive
        creatureClone:MoveTo(character.Torso.Position)

        -- If the Neck exists, teleport the character three studs above it.
        if (creatureNeck) then
            character.Torso.CFrame = (creatureNeck.CFrame + Vector3.new(0, 3, 0))
        end
    end
end

-- When SpawnCreature is fired by the client, run OnSpawnCreatureFired
SpawnCreature.OnServerEvent:Connect(OnSpawnCreatureFired)

Now, the reason for doing this with two scripts is this: Changes in the workspace made by LocalScripts will not replicate (be passed onto) other clients (players.) Your character would see the model, but others would not. By doing this with a ServerScript, we ensure that everything is done on the server, so that everyone sees the change. The RemoteEvent allows the client to tell the server when to spawn the creature, which is why it is so crucial.

I hope this helps. Let me know if anything comes up (I likely made a mistake. It's three in the morning...) Have a nice day, Aeike, and best of luck with your game!

Warm regards,

tkcmdr

Ad
Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
8 years ago
Edited 8 years ago

To teleport a Model, use the MoveTo function

a = game.ReplicatedStorage.AeikeTitan:Clone()
a.Parent = game.Workspace
a:MoveTo(game:GetService("Players").LocalPlayer.Character.Head.Position)

I think you can figure out what you want to do from there.

0
You're forgetting that this must be done by a server script in order for the model to replicate to other players. A server script must be made to do all that Aeike outlined; the only thing for the client to do is to tell the server script when to do the aforementioned. tkcmdr 341 — 8y
0
@tkcmdr This only depends on whether Workspace.FilteringEnabled is turned on. Link150 1355 — 8y
0
@Link150 Always assume it is. tkcmdr 341 — 8y

Answer this question