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 4 years ago
Edited 4 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?

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local banana1 = ReplicatedStorage.banana
03local player = game.Players.LocalPlayer
04local mouse = player:GetMouse()
05local Damage = 10
06script.Parent.Activated:Connect(function()
07    local banana = banana1:Clone()
08    banana.CFrame = script.Parent.Handle.CFrame
09 
10    local cow = Instance.new("BodyVelocity",banana)
11    cow.P = Vector3.new(100,100,100)
12    cow.Velocity = mouse.Hit.LookVector * 100
13    banana.Parent = game.Workspace
14    wait(2)
15    game.Workspace.banana:Destroy()
16end)
0
guess i will try use remote events then Mrpolarbear2games 55 — 4y

2 answers

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

wasnt solved gave up on this website

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

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

Local script (yours, currently).

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local banana1 = ReplicatedStorage.banana
03local player = game.Players.LocalPlayer
04local mouse = player:GetMouse()
05local Damage = 10
06 
07local RemoteEvent = ReplicatedStorage.RemoteEvent -- Please make a remote event in replicated storage.
08script.Parent.Activated:Connect(function()
09    RemoteEvent:FireServer(banana, Damage)
10end)

Server script -- Put it wherever you want.

01local RemoteEvent = game.ReplicatedStorage.RemoteEvent
02 
03RemoteEvent.OnServerEvent:Connect(function(banana, Damage)
04    local banana = banana1:Clone()
05    banana.CFrame = script.Parent.Handle.CFrame
06 
07    local cow = Instance.new('BodyVelocity', banana)
08    cow.P = Vector3.new(100, 100, 100)
09    cow.Velocity = mouse.Hit.LookVector * 100
10    banana.Parent = workspace
11 
12    banana.Touched:Connect(function(part) -- Touched event to do damage
13        local humanoid = part.Parent:FindFirstChild('Humanoid') -- Try to detect humanoid, returns nil if no humanoid is found.
14        if humanoid then -- Check to see if a valid humanoid is present
15            humanoid:TakeDamage(Damage)
16            -- Alternative: humanoid.Health -= Damage
17            -- ^^ Is the equivalent of humanoid.Health = humanoid.Health - Damage. Please use the comment above for cleaner code.
18        end
19    end)
20end)

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

Hope this helps. Cheers!

Answer this question