Every time I activate the tool, the character gets teleported 20 studs toward mouse position 1 more time.
So, after the first activation, the character is teleported 20 studs toward mouse position, but the second activation, it is teleported 40 studs and "teleported" is printed twice.
I originally made only one event, but I made another one because I thought that using the same event caused this effect, but this had no effect.
I also moved the teleport out of the repeat and used a flag instead, but it didn't have an effect either.
How can I make it so it always makes the character teleport once?
Tool Script:
local tool = script.Parent local character = workspace:WaitForChild(tool.Parent.Parent.Name) local player = game.Players:GetPlayerFromCharacter(character) local animator = character.Humanoid:FindFirstChildOfClass("Animator") local assets = tool.Assets local swing = assets.Swing local swingAnim = animator:LoadAnimation(swing) local mouseEvent = game.ReplicatedStorage.GetMousePos local mouseBack = game.ReplicatedStorage.BackMouseServer local hrp = character.HumanoidRootPart local torso = character.Torso tool.Activated:Connect(function() swingAnim:Play() wait(0.7) mouseEvent:FireClient(player) mouseBack.OnServerEvent:Connect(function(plr, mouseP) local overrideTp = false local targets = {} local rayDirection = mouseP.Unit * 20 local rayParams = RaycastParams.new() rayParams.FilterDescendantsInstances = {character:GetDescendants()} rayParams.FilterType = Enum.RaycastFilterType.Blacklist repeat local res = workspace:Raycast(torso.Position, rayDirection, rayParams) if res then local hitPart = res.Instance if hitPart.Parent:FindFirstChild("Humanoid") then table.insert(targets, res.Instance) rayParams.FilterDescendantsInstances = targets print("player hit") else local hitPoint = res.Position character.PrimaryPart.CFrame = CFrame.new(hitPoint) overrideTp = true end end until not res if overrideTp == false then character.PrimaryPart.CFrame = character.PrimaryPart.CFrame + (mouseP - hrp.Position).Unit * 20 print("teleporting") end end) end)
Local Script:
local player = game.Players.LocalPlayer local mouse = player:GetMouse() local event = game.ReplicatedStorage.GetMousePos local eventend = game.ReplicatedStorage.BackMouseServer local function getPosition() eventend:FireServer(mouse.Hit.p) end event.OnClientEvent:Connect(getPosition)