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

React to a Value when Projectile Impacts?

How do I code a script within a projectile that will do functions based on a value within it?

Basic idea:

There is a rocket launcher and a Value within the rocket containing the Username of the killer

When the rocket impacts, the person that died needs to do things based on the Username.Value within the Projectile Brick (IE, award a KO point the the right person)

How do I make the person that died recognize a projectiles findFirstChild:('Username').Value?

2 answers

well first for the projectile we can make it so that when it touches something it will either add a Value named killer inside Humanoid to the killer's name or it will update an existing one. Also instead of creating a new script for each projectile, lets just make the event fire inside the script you are creating the projectile

--script inside "gun"
local projectile = Instance.new("blah")
--change properties here
projectile.Touched:connect(funtion(hit)
	if hit.Parent:FindFirstChild("Humanoid")~=nil then
		if hit.Parent.Humanoid:FindFirstChild("Killer")~=nil then
			hit.Parent.Humanoid.Killer.Value=plr.Name
		else
			local k = Instance.new("ObjectValue")
			k.Value=plr
			k.Parent=hit.Parent.Humanoid
		end
		projectile:Destroy()
	end
end)

Now we can use the died event of humanoid to react to its killer

--script inside character
script.Parent:WaitForChild("Humanoid").Died:connect(function()
	if script.Parent.Humanoid:FindFirstChild("Killer")~=nil then
		local plr = script.Parent.Humanoid.Killer.Value
		--do stuff with player
		--ex: plr:LoadCharacter()
	end
end)

1 comment

0

I left an update to the code I'm stuck on vvvv

I almost have it figured out. For some reason, the first time it kills a user, I get an error. After that, it works. How could I get rid of the error? Error:

13:26:42 -- Players.Moxeh.Backpack.RocketLauncher.ServerRocketManager:140: attempt to index local 'WorkspaceHitUser' (a nil value)
13:26:42 -- Stack Begin
13:26:42 -- Script 'Players.Moxeh.Backpack.RocketLauncher.ServerRocketManager', Line 140
13:26:42 -- Stack End
13:27:00 -- Moxeh KILLED: warlegion

The code:

local function OnHitPlayers(player, hitPlayers)
	for hitPlayerId, _ in pairs(hitPlayers) do
		DamagePlayer(hitPlayerId, player, Configurations.SplashDamage.Value)
		
		--MOXEH
		local LocalPlayer = game.Players.LocalPlayer
		local HitId = script.HitUserId
		local HitName = script.HitName
		local WorkspaceHitUser = game.Workspace:FindFirstChild(HitName.Value)
		
		HitId.Value = hitPlayerId
		wait()

		if WorkspaceHitUser.Humanoid.Health <= 0 then
			print (player.Name, 'KILLED:', HitName.Value)
		
			end
		
		--MOXEH
	end
end

1 comment

0

on line 14, if WorkspaceHitUser~=nil and WorkspaceHitUser.Humanoid.Health <= 0 then

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