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?
01 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local banana 1 = ReplicatedStorage.banana |
03 | local player = game.Players.LocalPlayer |
04 | local mouse = player:GetMouse() |
05 | local Damage = 10 |
06 | script.Parent.Activated:Connect( function () |
07 | local banana = banana 1 :Clone() |
08 | banana.CFrame = script.Parent.Handle.CFrame |
09 |
10 | local cow = Instance.new( "BodyVelocity" ,banana) |
11 | cow.P = Vector 3. new( 100 , 100 , 100 ) |
12 | cow.Velocity = mouse.Hit.LookVector * 100 |
13 | banana.Parent = game.Workspace |
14 | wait( 2 ) |
15 | game.Workspace.banana:Destroy() |
16 | end ) |
wasnt solved gave up on this website
Try to use touched events. This is how I would approach it (with remote events of course).
Local script (yours, currently).
01 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local banana 1 = ReplicatedStorage.banana |
03 | local player = game.Players.LocalPlayer |
04 | local mouse = player:GetMouse() |
05 | local Damage = 10 |
06 |
07 | local RemoteEvent = ReplicatedStorage.RemoteEvent -- Please make a remote event in replicated storage. |
08 | script.Parent.Activated:Connect( function () |
09 | RemoteEvent:FireServer(banana, Damage) |
10 | end ) |
Server script -- Put it wherever you want.
01 | local RemoteEvent = game.ReplicatedStorage.RemoteEvent |
02 |
03 | RemoteEvent.OnServerEvent:Connect( function (banana, Damage) |
04 | local banana = banana 1 :Clone() |
05 | banana.CFrame = script.Parent.Handle.CFrame |
06 |
07 | local cow = Instance.new( 'BodyVelocity' , banana) |
08 | cow.P = Vector 3. 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 ) |
20 | end ) |
If you don't know what RemoteEvents are, read this documentation.
Hope this helps. Cheers!