I've been trying to figure out what do to when the mouse is out of the max range for the fire shower thingy. The question I wanted to ask is how do I code, for example, when the mouse goes out of the max range it'll just set the position of the fireshower to the direction of the mouse but on the border of the max range. I hope I explained it clearly because I don't know if I did. Here's the whole script below:
wait(4) script.Parent.Ferid.OnServerEvent:Connect(function(Player, Mouse) local maxrange = 50 script.Parent.Parent.Parent.Data.Tries.Value = script.Parent.Parent.Parent.Data.Tries.Value + 30 local AlreadyTouched = false local Character = Player.Character or Player.CharacterAdded:wait() local torso = Character.UpperTorso local Shower = game.ReplicatedStorage.FireShower:Clone() Shower.Parent = game.Workspace for i,v in pairs(Shower:GetDescendants()) do if v:IsA("Part") then local distance = (Mouse.Position-torso.Position).Magnitude if distance < maxrange then v.CFrame = v.CFrame + Mouse*Vector3.new(Mouse.Position,Mouse.Position,Mouse.Position) else --code when it's out of maxrange should be put here end --This part's the line of code that controls the shower's position. end v.Touched:Connect(function(hit) if hit.Parent.Name ~= nil then local yo = hit.Parent:findFirstChild("Humanoid") if yo == Character then yo.Health = yo.Health - 0 elseif hit.Parent.Name ~= nil and hit.Parent ~= Shower:GetDescendants() then local tween = game:GetService("TweenService") local boom = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0) local boomprop = {Anchored = true;CanCollide = false;Transparency = 1;Size = Vector3.new(48,48,48) } local ew = tween:Create(v,boom,boomprop) ew:Play() wait(1.5) Shower:Destroy() end end end) end end)
Use Raycasting it's simple and somewhat easy.
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - toolHandle.CFrame.p).unit * 50) -- you can find the end position of the ray by creating a variable local endPos = ray.Origin + ray.Direction end
read the ROBLOX wiki on Raycasting to get a general understanding of it.
just use magnitude which returns the length of a vector so you could subtract 2 vectors and get the length between them.
ex
local player = game.Players.LocalPlayer local character = player.Character or player.CharacterAdded:Wait() local mouse = player:GetMouse() local maxRange = 50 -- max range of 50 studs local distance = (mouse.Hit.Position, character.HumanoidRootPart.CFrame.Position).Magnitude if distance <= maxRange then -- if the distance is less or equal to the max range then print("in range!!!!") end