• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


28 Nov, 2024

Updated at 02 Dec, 2024

Part going towards the camera

Hey i already reported this on the devforum but im posting this again cuz the old script didnt work excactly right for me so ima making a new one

I have two scripts in my Roblox game. One script fires a RemoteEvent, and the other controls a part that is cloned from ReplicatedStorage and made to follow the player’s mouse. The script works, but there’s a glitch.

In the video below, you can see that the part moves towards the camera when I stop moving my mouse. However, when I move the mouse again, the part returns to its correct position.

Can anyone help me fix this? I want the part to continuously follow the mouse, without moving toward the camera when the mouse stops moving.

Server Script

local tool = script.Parent
local tweenService = game:GetService("TweenService")
local remoteEvent = tool:WaitForChild("TeleportEvent")
local replicatedStorage = game:GetService("ReplicatedStorage")

local part = nil
local lastMousePosition = nil
local partTemplate = replicatedStorage:WaitForChild("Part")

local function onTeleportRequest(player, mousePosition)
	if player.Backpack:FindFirstChild(tool.Name) or player.Character:FindFirstChild(tool.Name) then
		if not part then
			part = partTemplate:Clone()
			part.Position = player.Character.HumanoidRootPart.Position
			part.Anchored = true
			part.CanCollide = false
			part.Parent = workspace
		end

		if not lastMousePosition or (mousePosition - lastMousePosition).Magnitude > 0.1 then
			local goal = {Position = mousePosition}
			local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
			local tween = tweenService:Create(part, tweenInfo, goal)
			tween:Play()
			lastMousePosition = mousePosition
		else
			local offset = (mousePosition - part.Position).Unit * 0.1
			local newPos = part.Position + offset
			local goal = {Position = newPos}
			local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
			local tween = tweenService:Create(part, tweenInfo, goal)
			tween:Play()
		end
	else
		-- Destroy the part if the player is no longer holding the tool or it is not equipped
		if part then
			part:Destroy()
			part = nil
		end
	end
end

remoteEvent.OnServerEvent:Connect(onTeleportRequest)

Localscript

local player = game.Players.LocalPlayer
local userInputService = game:GetService("UserInputService")
local tool = script.Parent
local remoteEvent = tool:WaitForChild("TeleportEvent")

local isHoldingT = false

local function onKeyPress(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.T and not gameProcessedEvent then
		isHoldingT = true
		while isHoldingT do
			local mousePos = player:GetMouse().Hit.p
			remoteEvent:FireServer(mousePos)
			wait(0.1)
		end
	end
end

local function onKeyRelease(input)
	if input.KeyCode == Enum.KeyCode.T then
		isHoldingT = false
	end
end

userInputService.InputBegan:Connect(onKeyPress)
userInputService.InputEnded:Connect(onKeyRelease)

if somone needs file to the game i can give it to you

1 post - 1 participant

Read full topic