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

How do i make this gun actually do damage!?

Asked by 3 years ago
Edited 3 years ago

So i made a gun that shoots bananas but i want them to do damage when they hit a player. I have already tried just making a simple damage script in the banana its self but it doesnt work? Any ideas?


local ReplicatedStorage = game:GetService("ReplicatedStorage") local banana1 = ReplicatedStorage.banana local player = game.Players.LocalPlayer local mouse = player:GetMouse() local Damage = 10 script.Parent.Activated:Connect(function() local banana = banana1:Clone() banana.CFrame = script.Parent.Handle.CFrame local cow = Instance.new("BodyVelocity",banana) cow.P = Vector3.new(100,100,100) cow.Velocity = mouse.Hit.LookVector * 100 banana.Parent = game.Workspace wait(2) game.Workspace.banana:Destroy() end)
0
guess i will try use remote events then Mrpolarbear2games 55 — 3y

2 answers

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

wasnt solved gave up on this website

Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Try to use touched events. This is how I would approach it (with remote events of course).

Local script (yours, currently).

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local banana1 = ReplicatedStorage.banana
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Damage = 10

local RemoteEvent = ReplicatedStorage.RemoteEvent -- Please make a remote event in replicated storage.
script.Parent.Activated:Connect(function()
    RemoteEvent:FireServer(banana, Damage)
end)

Server script -- Put it wherever you want.

local RemoteEvent = game.ReplicatedStorage.RemoteEvent

RemoteEvent.OnServerEvent:Connect(function(banana, Damage)
    local banana = banana1:Clone()
    banana.CFrame = script.Parent.Handle.CFrame

    local cow = Instance.new('BodyVelocity', banana)
    cow.P = Vector3.new(100, 100, 100)
    cow.Velocity = mouse.Hit.LookVector * 100
    banana.Parent = workspace

    banana.Touched:Connect(function(part) -- Touched event to do damage
        local humanoid = part.Parent:FindFirstChild('Humanoid') -- Try to detect humanoid, returns nil if no humanoid is found.
        if humanoid then -- Check to see if a valid humanoid is present
            humanoid:TakeDamage(Damage)
            -- Alternative: humanoid.Health -= Damage
            -- ^^ Is the equivalent of humanoid.Health = humanoid.Health - Damage. Please use the comment above for cleaner code.
        end
    end)
end)

If you don't know what RemoteEvents are, read this documentation.

Hope this helps. Cheers!

Answer this question