Hey devs!
My issue is I can’t get it to work. I’m making a melee weapon that will be used specifically for a boss fight, and I want it to deal damage to the boss when in a 8 stud range.
I’ve tried using Vector3 and DistanceFromCharacter, but I assumed DistanceFromCharacter wouldn’t work as the boss isn’t a player.
This is the entire script:
local gun = script.Parent
local runService = game:GetService("RunService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local userInputService = game:GetService("UserInputService")
--Objects
local viewModel = replicatedStorage:WaitForChild("MainGame"):WaitForChild("Guns"):WaitForChild("Sword"):Clone()
local camera = workspace.CurrentCamera
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local h = character:WaitForChild("Humanoid") or character:FindFirstChild("Humanoid") or nil
local mouse = player:GetMouse()
viewModel.Parent = replicatedStorage.UndequippedGuns
viewModel.Name = "ViewModel"
--Modules
local config = require(viewModel:WaitForChild("Config"))
--//Main Code\\
local cf = CFrame.new()
local sinSpeed = 8
local sinSize = 6
local cosSpeed = 15
local cosSize = 8
local recoilSpeed = 10
local recoilSize = 1
local equipConnection
local shootCooldown = false
local isHolding = false
local isEquipped = false
local animations = {
["Hold"] = viewModel.AnimationController.Animator:LoadAnimation(viewModel.Animations.Hold);
["Inspect"] = viewModel.AnimationController.Animator:LoadAnimation(viewModel.Animations.Inspect);
["Slash"] = viewModel.AnimationController.Animator:LoadAnimation(viewModel.Animations.Slash);
}
animations["Hold"]:Play()
cf = config.OffsetFromCamera
function positionModel()
if player.character then
viewModel:SetPrimaryPartCFrame(camera.CFrame*cf)
if player.character.Humanoid.MoveDirection.Magnitude > 0 then
local sin = math.sin(time() * sinSpeed)/sinSize
local cos = math.cos(time() * cosSpeed)/cosSize
cf = cf:Lerp(CFrame.new(config.OffsetFromCamera.X+sin, config.OffsetFromCamera.Y+cos,config.OffsetFromCamera.Z),.2)
else
cf = cf:Lerp(config.OffsetFromCamera,0.2)
end
end
end
userInputService.InputBegan:Connect(function(input,GPE)
if input.KeyCode == Enum.KeyCode.F then
animations["Inspect"]:Play()
wait(8)
animations["Hold"]:Play()
end
end)
userInputService.InputBegan:Connect(function(input,GPE)
if input.KeyCode == Enum.KeyCode.R then
animations["Inspect"]:Stop()
wait(0.12)
animations["Hold"]:Play()
end
end)
function shoot(target)
if not shootCooldown then
shootCooldown = true
animations["Inspect"]:Stop()
if player.Character:FindFirstChild(script.Parent.Name) ~= nil then
local sin = math.sin(time() * recoilSpeed)/recoilSize
cf = cf:Lerp(CFrame.new(config.OffsetFromCamera.X, config.OffsetFromCamera.Y,config.OffsetFromCamera.Z+sin),.2)
viewModel.Shoot:Play()
end
if target ~= nil then
if target.Parent:FindFirstChild("Humanoid") then
replicatedStorage["MainGame"]["Remotes"]["Shoot"]:FireServer(mouse.Target, gun.Name)
end
end
wait(config.Cooldown_Between_Each_Click)
shootCooldown = false
end
end
gun.Activated:Connect(function()
if config.IsAutomatic == false then
shoot(mouse.Target)
end
end)
mouse.Button1Down:Connect(function()
if config.IsAutomatic == true then
isHolding = true
end
end)
mouse.Button1Up:Connect(function()
if config.IsAutomatic == true then
isHolding = false
end
end)
local function equip()
if not gun and viewModel then return end
for _, part in pairs(gun:GetChildren()) do
if part:IsA("BasePart") or part:IsA("UnionOperation") then
part.Transparency = 1
end
end
viewModel.Parent = camera
animations["Hold"]:Play()
equipConnection = runService.RenderStepped:Connect(function()
if player.character.Humanoid.Health > 0 then
positionModel()
end
end)
end
local function unequip()
if not gun then return end
equipConnection:Disconnect()
viewModel.Parent = replicatedStorage.UnequippedModels
end
gun.Equipped:Connect(function()
mouse.Icon = "http://www.roblox.com/asset?id=5054846840"
animations["Inspect"]:Stop()
isEquipped = true
equip()
end)
--Runs the unequip function
gun.Unequipped:Connect(function()
unequip()
isEquipped = false
mouse.Icon = "rbxasset://SystemCursors/Arrow"
end)
runService.RenderStepped:Connect(function()
if isHolding == true and isEquipped == true then
shoot(mouse.Target)
end
end)
And I want to check the range here at the very bottom:
runService.RenderStepped:Connect(function()
if isHolding == true and isEquipped == true then
shoot(mouse.Target)
end
end)
Any help is appreciated!
Best regards,
RoboBoy013