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

Why doesn't the first function run?

local Humanoid = script.Parent:WaitForChild("Humanoid")	
local currentHealth = Humanoid.Health	
local RewardGold = script.Parent.Rewards.Gold.Value
local RewardExp = script.Parent.Rewards.Exp.Value	
local Player = game.Players.LocalPlayer
local Gold = game.ReplicatedStorage:WaitForChild(Player.Name.." Values").Gold
local Exp = game.ReplicatedStorage:WaitForChild(Player.Name.." Values").Exp
--Variables

Humanoid.Died:connect(function()    --This function Doesn't Run
	print("This Will Display if Function Runs")

	--Changes an IntValue in ReplicatedStorage
	Gold.Value = Gold.Value + 5			
	Exp.Value = Exp.Value + 1
end)

Humanoid.HealthChanged:connect(function(Health)			--This Runs
	local Change = math.floor(currentHealth - Health)
	local Gui = script.Parent.Head:FindFirstChild("Damage")
	if Change > 0 then
		if Gui == nil then
			Gui = Instance.new("BillboardGui", script.Parent.Head)
			Gui.Name = "Damage"
			Gui.Size = UDim2.new(0,100,0,10)
			Gui.StudsOffset = Vector3.new(0,3,0)
			Gui.MaxDistance = 20
			
			--This Will Display Damage Player has done
			local DamageIndicator = Instance.new("TextLabel", Gui)      
			DamageIndicator.TextColor3 = Color3.new(255,255,255)
			DamageIndicator.Text = Change
			DamageIndicator.BackgroundTransparency = 1
			DamageIndicator.Size = UDim2.new(1,0,1,0)
			currentHealth = math.floor(math.abs(currentHealth - Change))
			wait(1)
		end
	end
	if Gui ~= nil then
		Gui:Remove()
	end
end)

This is a localscript inside of an npc

i created a print to test if the first function runs at all and nothing is displayed in output. No errors are shown either. I also have filtering enabled if that is doing something.

what am i doing wrong?

1 comment

0

I don't know what's wrong. I just know that you should use :Connect() and not :connect() since it is depreciated.

2 answers

✓ Accepted answer

(This is an extension of what WillieTehWierdo200 said and your comment reply)

You have to decide how you're going to figure out who the player who attacked the NPC is. A few options:

  • The original/old Roblox weapons used a 'Tag' system on projectiles. If damage is done, the Tag would be moved to the victim's humanoid (and I think after a wait() was removed). If that player died in that instant, the leaderboard would be able to see the Tag and know who was responsible. You could do the same. You could also leave Tags for longer, and/or have them keep track of how much damage players have inflicted
  • You could use a RemoteEvent for a player to tell the server that they wish to damage an NPC's health. Note that an exploiter could use this to instant-kill all NPCs if you don't perform checks to try and minimize this. (If you don't have FilteringEnabled on, then you needn't worry about RemoteEvent security because your entire place could be destroyed in any online server by a single exploiter.)

Note: :Remove is deprecated (use .Visible = false instead -- don't forget to make it visible when more damage comes in). :connect is also deprecated, use :Connect.

1 comment

0

Okay i got the second option to work as my game doesn't use projectiles (yet?). thanks for the help and tips

LocalScripts won't run in the Workspace, where I assume your NPC is. Try changing to a regular Script.

1 comment

0

Okay the function now runs but how would i get the player's name without using game.players.localplayer?

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