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

How can I make my projectile system deal damage?

I have made a projectile system and here is how it works.

A client fires the gun, a bullet is then made LOCALLY on the client that fired it. The client then fires a remote event with the root position of the bullet, and the target position of the bullet to the server. The server then sends those cframes down to every client so they can also render the bullet. I have successfully made this to where other players can see the bullets that another player fires, but I have no clue how to make them deal damage. I have tried to put a localscript in the bullet that fires a remote event whenever it touches a player, so the server can deal damage, but that did not work. All help is appreciated. Thank you.

--Server

local RS = game:GetService("ReplicatedStorage")
local RE = RS:WaitForChild("FireEvent")

RE.OnServerEvent:Connect(function(player, targetPos, rootPos)
	local initialPlayer = player
	RE:FireAllClients(initialPlayer, targetPos, rootPos)
end)

--Client

local RS = game:GetService("ReplicatedStorage")
local RE = RS:WaitForChild("FireEvent")
local player = game.Players.LocalPlayer
local bul = RS:WaitForChild("Bullet")

RE.OnClientEvent:Connect(function(initialPlayer, targetPos, rootPos)
	if initialPlayer ~= player then
		local clone = bul:Clone()
		clone.Parent = game.Workspace
		clone.CFrame = rootPos
		clone.CFrame = CFrame.new(clone.Position, targetPos)
		clone.BodyVelocity.velocity = clone.CFrame.LookVector * 120
	end
end)

--Inside of the tool that fires the projectile 

local db = false
local handle = script.Parent:WaitForChild("Handle")
local RS = game:GetService("ReplicatedStorage")
local bul = RS:WaitForChild("Bullet")
local RE = RS:WaitForChild("FireEvent")

script.Parent.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if db == false then
			db = true
			local char = script.Parent.Parent
			local mH = mouse.Hit
			local rootPos = script.Parent.Handle.CFrame * CFrame.new(char.UpperTorso.CFrame.LookVector * 2)
			local targetPos = mH.p
			RE:FireServer(targetPos, rootPos)
			local clone = bul:Clone()
			clone.Parent = game.Workspace
			clone.CFrame = rootPos
			clone.CFrame = 	CFrame.new(clone.Position, mH.p)
			clone.BodyVelocity.velocity = clone.CFrame.LookVector * 120
			wait(0.3)
			db = false
		end
	end)
end)

7 comments

0

the blast must have an hit then hit finds a player that has a humanoid, and then hit damages that using :TakeDamage(HEALTHNUMBER)

0

I tried that but it did nothing to the other player. I put the hit detection script inside the bullet.

0

this is why u shouldnt do stuff like this

0

u try to make it faster and u end up with a lot more crap then u started with

0

also u dont need to send the root pos u could get that from the player's character

0

every bit counts uk

0

also also, what ur trying to do is practically what the server does when u spawn a part so there is really not much difference, tho its does make it more unbalanced for ppl with faster internet cus they get to see the bullet coming first

2 answers

idk if u really tried this or not; u didnt state it clearly. but the best way to make the hit detection would be to have a detection script in the original bullet. and ye thats it(remember; original!)

3 comments

0

I've tried that but since the bullets are client sided they do not damage.

0

I just made it so the bullets don't do damage. I set up raycasting on the server for hit detection, now the bullets are just visual effects.

0

ok but u have a remote event send it to the server

I guess this is my answer. But an explanation: What type of player you are hitting? An NPC or a real Roblox player? Does it has Humanoid? I guess my code does not work then:

 --the code you did
wait(0.1)
clone.Touched:Connect(function(hit) --hit is the thing to use to detect
local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid") --maybe the Humanoid has a different name. Also I added parent because it may be a model.
if humanoid then
print("Found") 
--clone:Destroy() --you can remove this, or add an explosion.
humanoid:TakeDamage(Put your own number)
end
end)

I don't know what to prevent the caster being hurt. If this does not work. It's okay!

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