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

I need help with coding a max range for my elemental game. Does anyone know how?

Asked by 5 years ago

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)
0
magnitude tacotown2 119 — 5y

2 answers

Log in to vote
3
Answered by 5 years ago
Edited 5 years ago

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.

0
Yo Thanks for this, it really helped xXKingCool258Xx 39 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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
0
But then how do I make the fire shower spawn on the border of the max range if the mouse were to go out of range? xXKingCool258Xx 39 — 5y
0
you could raycast to the mouse's position and multiply the unit by 50 (the max range) then you could use the intersection vector3 value that workspace:FindPartOnRay() returns and use that as the border User#23365 30 — 5y

Answer this question