Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i make this gun actually do damage!?

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)

1 comment

0

guess i will try use remote events then

2 answers

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).

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.