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

How Do I Stop Raycast From Duplicating?

Asked by
Ashedee 43
3 years ago

Hello, I Want to start by saying I am new to raycasting

The script is working for the most point as it wont run unless I am within 10 units. However if I click with or without the tool equipped it adds to a queue and then when I click on the humanoid it apply 30dmg or 50 dmg (how ever many times I click) instead of 1

I would like this script to only apply 1 dmg to the humanoid when I am within the 10 units and do nothing when I am farther away

Local Script

local mouse = game.Players.LocalPlayer:GetMouse()
local tool = script.Parent
local Gun = false

mouse.Button1Down:Connect(function()
    local function onSelected() -- Player Starts Using The Tool     
        script.Parent.Fire:FireServer(mouse.Hit.p)
    end 
    local function onDeselected() -- Player Stops Using The Tool 

    end 

    tool.Unequipped:Connect(function() Gun = false end)
    tool.Activated:Connect(onSelected) 
    tool.Deactivated:Connect(onDeselected)
end)

Server Script

script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {player.Character}
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

    local raycastResult = workspace:Raycast(script.Parent.Handle.Position, (mousePos - script.Parent.Handle.Position)*100,raycastParams)
    if raycastResult then
        local hitPart = raycastResult.Instance
        local model = hitPart:FindFirstAncestorOfClass("Model")
        if model then
            if model:FindFirstChild("Humanoid") then
                if (model.HumanoidRootPart.Position - script.Parent.Handle.Position).magnitude < 10 then
                    model.Humanoid:TakeDamage(1)
                    print("Gott EEMM")
                else
                    print("To Far Away")
                end
            else
                print("Not Vaild Target")
            end
        end

    end 
end)

Answer this question