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

why isnt my projectile system damaging ontouch?

ok. i made a script that lets you shoot projectiles when you press the key "e" now i need the projectiles to damage npcs on touch. i tried putting every damage script out there in the projectile, nothing worked. heres the script:

local sound = script.fire 
local tweenservice = game:GetService("TweenService") 
local plr = game.Players.LocalPlayer

local tear1 = game.ReplicatedStorage.Player1tear 
local UIS = game:GetService("UserInputService") 

UIS.InputBegan:connect(function(input) 
if input.KeyCode == Enum.KeyCode.E then 
print("Working") 
sound:Play() 
local clonetear = tear1:Clone() 
clonetear.Parent = game.Workspace 
clonetear.CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.lookVector * 5 tweenservice:Create(clonetear,TweenInfo.new(1,0,0),{CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.lookVector * 40,Transparency = 1},Enum.EasingStyle.Quint,Enum.EasingDirection.Out):Play() 
game.Debris:AddItem(2.5,clonetear) -- change the number of time to wait until the ball dissapear,this can be the ball range untill get deleted
end 
end)

keep in mind: theres a projectile in replicated storage called tear1 theres a sound in the script called fire the localscript in the codeblock goes into starterplayerscripts. any help?????

2 answers

Seems like you're missing a .Touched which will trigger when the projectile hits something.

UIS.InputBegan:connect(function(input) 
    if input.KeyCode == Enum.KeyCode.E then 
        print("Working") 
        sound:Play() 
        local clonetear = tear1:Clone()
        
        -- Touched event
        clonetear.Touched:Connect(function(hit)
            local hitHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid") -- Check for a humanoid
            if hitHumanoid then 
                -- You'll need to set up some remote events to deal real damage.
                hitHumanoid:TakeDamage(10) -- Deal damage
                game.Debris:AddItem(0, clonetear) -- Remove projectile
            end
        end)
        
        clonetear.Parent = game.Workspace 
        clonetear.CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.lookVector * 5
        tweenservice:Create(clonetear,TweenInfo.new(1,0,0),{CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.lookVector * 40,Transparency = 1},Enum.EasingStyle.Quint,Enum.EasingDirection.Out):Play() 
        game.Debris:AddItem(2.5,clonetear)
    end 
end)

6 comments

0

im still a starter so i don't know how to set up remote events. explanation please?

0

there wasn't any remote events in her answer. basically, remote events can transfer data from client to server, and vice versa.

0

i meant his

0

He said in the script --youll need to set up some remote events to deal real damage---

0

actually i set this script up and it kinda woks. it kills the npc instantly. is thee a way to deal like 10 damage or something?

0

okay this is interesting. i set up the remote event, and now the projectiles damage me when they touch the npc??????

You will need to use the BasePart.Touched event in the projectile. If you don't know what the Touched even is, it fires when a part touches it.

This script below is the same as @SimpleFlame's answer but I modified a bit.

-- in client script
UIS.InputBegan:Connect(function(input, isChatting)
	if not isChatting then -- checks if the player is not typing in chat
		if input.KeyCode == Enum.KeyCode.E then
			print("Working")
			sound:Play()
			local clonetear = tear1:Clone()
			
			clonetear.Touched:Connect(function(hit)
				local hitHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
				local hitPlayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) -- checks if it's a player
				
				if hitHumanoid and not hitPlayer then -- checks if it's not a real player
					game:GetService("ReplicatedStorage"):WaitForChild("DealDamage"):FireServer(10) -- Deal damage
					game.Debris:AddItem(0, clonetear) -- Remove projectile
				end
			end)
			
			clonetear.Parent = game.Workspace
			clonetear.CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.lookVector * 5
			clonetear.Velocity = clonetear.CFrame.lookVector * 40 -- shoots the projectile
			tweenservice:Create(clonetear,TweenInfo.new(1,Enum.EasingStyle.Quint,Enum.EasingDirection.Out),{Transparency = 1}):Play()
			game.Debris:AddItem(2.5,clonetear)
		end
	end
end)

-- in server script (ServerScriptService)

game:GetService("ReplicatedStorage"):WaitForChild("DealDamage").OnServerEvent:Connect(function(player, damage)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	
	humanoid:TakeDamage(damage)
end)

9 comments

0

when i copy it there ae some red lines on the line 20 is anot a typo?

0

line 12*

0

and also if you can, make the damage a value, so it can be changed by lets say touching a part

0

Oh right sorry abt that, I fixed line 12

0

Oh, about @SimpleFlame saying about using remote events to damage players, it's because if player died in client script it wont always send to the server. So yes, you do need to make remote events. Make one in ReplicatedStorage named "DealDamage" and copy my script.

0

thanks! ill try it as soon as i get a chance, ill let you know if ot works

0

ummmm now the projectiles have gravity fsr, dont deal damage like before and dont dissapear. hmmm

0

did you pasted the whole script? pls look down at the script. you can see "in server script" line, remove that line from the client and put that line in a script in serverscriptstorage

0

i did. the script from before actually dealt damage, now i just need the projectiles to dissapear

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