Would this script work?

Would this script work?

The first is a LocalScript in StarterCharacterScripts, the second is a normal script in ServerScriptService.

-- In StarterCharacterScripts, LocalScript.

local ts = game:GetService("TweenService")
local char = script.Parent
local mouse = plr:GetMouse()

local event = game.ReplicatedStorage.AttackEvent
local canAttack = true

function reload()
  canAttack = false
  wait(RELOAD_TIME)
  canAttack = true
end

mouse.Button1Down:Connect(function()
  if (char.HumanoidRootPart.Position - mouse.CFrame.Position).Magnitude <= 30 then
    if canAttack then
      event:FireServer(mouse.CFrame.Position, char.HumanoidRootPart.Position)
      reload()
    end
  end
end

ServerScript…

-- In ServerScriptService, Normal Script.

local ts = game:GetService("TweenService")
local event = game.ReplicatedStorage.AttackEvent
local STUDS_AWAY = 3 -- The amount of studs away you want the part to start away from the character.
local RANGE = 30 -- The range of the attack
local SPEED_OF_ATTACK = 3 -- 3 studs per second

event.OnServerEvent:Connect(function(plr, mousepos, playerpos)
  local distance = (playerpos - mousepos).Magnitude
  if distance <= RANGE and distance > STUDS_AWAY then
    local attack = Instance.new("Part", game.Workspace)
   
    attack.Name = "Attack"
    attack.Shape = "Ball"
    attack.Position = playerpos:lerp(mousepos, STUDS_AWAY/distance)

    local Properties = {
      position = mousepos
    }

    local Info = TweenInfo.new(distance/SPEED_OF_ATTACK, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 1, false, 0)

    local tween = ts:Create(attack, Info, Properties):Play()
    
    attack:Destroy()
  end
end

I just wanted to know.

Maybe someone can test it?

1 Like

2 questions:

  1. What’s this script supposed to do?
  2. Why can’t you test it yourself to see if it works?
1 Like
  1. This script is supposed to, when one clicks, create a part and move it in the direction of mousepos, at the speed of SPEED_OF_ATTACK.

  2. Reasons…

Bumping this topic… heh
(char limit)

This is really something you should test yourself, but there are actually quite a few issues.

Both scripts are missing a closing “)” as the final character since you are doing anonymous functions inside a Connect.

RELOAD_TIME is never set in the local script

It is mouse.Hit. mouse.CFrame will error. I would advise against using the mouse object though, it’s generally better to use UserInputService.

Your server script would immediately delete the attack object after starting the tween because there is no yielding.
Also, tween is nil since :Play() doesn’t return anything. Not a big deal unless you want to yield with a tween.Completed:Wait()

Finally someone testing this script wouldn’t have the remoteEvent which would cause it to error.

Other than that though it would likely work. (though you might want to consider anchoring attack)

1 Like

so… instead of this:

local tween = ts:Create():Play

This:

local tween = ts:Create()
tween:Play()

Yeah. But that’s just so you can follow it up with the next line.

local tween = ts:Create()
tween:Play()
tween.Completed:Wait()

So that your attack object will only be destroyed on tween completion.

1 Like

Yes, when I made this script, I was kinda rushing… heh

2 Likes