Hey everyone! I’m trying to make an object clone from ReplicatedStorage to in front of the player, but the script I’m using doesn’t work correctly, and I’m not sure why:
Object always teleport to this same position
Here’s the script I’m using (inside StartPlayerScript)
local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait() or player.Character
local InputService = game:GetService("UserInputService")
local head = character:WaitForChild("Head").CFrame
local pPos = head.LookVector
local pDistance = 3
local PortalPos: Vector3 = head.Position + (pPos * pDistance)
local debounce = false
InputService.InputBegan:Connect(function(InputKey, Processed)
if InputKey.KeyCode == Enum.KeyCode.E then
game.ReplicatedStorage.TimePortal.Position = PortalPos
game.ReplicatedStorage.TimePortal:Clone().Parent = game.Workspace
print("cloned")
end
end)
local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait() or player.Character
local InputService = game:GetService("UserInputService")
local head = character:WaitForChild("Head").CFrame
local pPos = head.LookVector
local pDistance = 3
local PortalPos = head.Position + Vector3.new(pPos * pDistance)
local debounce = false
InputService.InputBegan:Connect(function(InputKey, Processed)
if InputKey.KeyCode == Enum.KeyCode.E then
game.ReplicatedStorage.TimePortal:Clone(game.Workspace).Position = PortalPos
print("cloned")
end
end)
local TimePortal : Instance = ...
local NewTimePortal = TimePortal:Clone()
NewTimePortal.CFrame = Character:GetPivot() + CFrame.new(0, 0, 3)
-- or if the portal is a model which requires a PrimaryPart
NewTimePortal:PivotTo(Character:GetPivot() + CFrame.new(0, 0, 3))
You are changing the position of the portal in ReplicatedStorage instead of doing so after cloning it. Try with:
InputService.InputBegan:Connect(function(InputKey, Processed)
if InputKey.KeyCode == Enum.KeyCode.E then
local Portal = game.ReplicatedStorage.TimePortal:Clone(game:GetService("Workspace"))
Portal.Position = PortalPos
print("Cloned")
end
end)