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

How To Make The Hostile NPC Recognize and gives exp to the killer?

hello there ,

so recently im planning to make a rpg game and when testing using roblox studio play mode but im having trouble with the stats system that gives exp when a player killed a npc

the script in the weapon (using regular script and it also says the player cant be found) :

 local tool = script.Parent
local candamage = tool:WaitForChild("CanDamage")
local player = game.Players.LocalPlayer

tool.Blade.Touched:connect(function(p)
	local char = p.Parent
	local hum = char:FindFirstChild("Humanoid")
	local hitsound = tool:WaitForChild("Hit")
if candamage.Value == false then
	if hum then
			candamage.Value = true
			hitsound:Play()
			hum.Health = hum.Health - script.Dmg.Value
			wait(1)
			candamage.Value = false
			local tag = Instance.new("StringValue",p.Parent.Humanoid) -- creates new value
        tag.Name = "creator" -- sets the name
        tag.Value = player.Name -- sets it to the name of the killer (i hope)
		end
end
end)

the one inside the npc (using script):

local human = script.Parent:WaitForChild("Humanoid")
human.Died:connect(function()
    local tag = human.Parent:FindFirstChild("creator")
    if tag then
game.ReplicatedStorage.Events.Geee:Fire(10,human.creator.Value)--gee is a bindable event so far i haven't got an error in the output but if there's a mistake please help me :( im still new to scripting
    end
end)

it keeps saying it cant find the creator tag

i hope it can give the player 10 exp when the player kills it

Edit: I saw the comments that i should use Event to add the tag to the npc but how do i exactly do that?

11 comments

0

Does you game use FE ? If yes the first script (in the weapon) should be a local script because Players.LocalPlayer can't be accessed with a regular script. And you should use remote event/function to spawn the tag.

0

kid make the npc remember the person that last hit it, then once the npc dies award points to the player. this encourages competition in the game.

0

your making local tag = Instance.new("StringValue",p.Parent.Humanoid) have a parent early

0

Also instead of hum.Health = hum.Health - script.Dmg.Value, use hum:TakeDamage(script.Dmg.Value)

0

@TheluaBanana thats what im trying to ask

0

@xJathur95x Roblox Forced FE on all games on 2018 and thx for the suggestion to ill try to make it that way

0

@RichardP965 good job u dont even know how to do that

0

its ok well get there step by step

0

@TheluaBanana Can you teach me how to do that correctly?

0

k i gave a easy enough way

2 answers

✓ Accepted answer
  1. place string value inside npc

  2. when player uses a weapon or something, and if that weapon hits the npc, have the string value mentioned in (1.) set to the player's name

  3. if npc dies have a script in it which searches thru the player list to find the player with matching name of the value, and if match is found, award player points

simple enough?

2 comments

0

yea its thanks but i don't understand the (1.) is it meant to be the first script?

0

no its meant to be the first step.

u changed

local char = p.Parent

u need change it to

local char = p.Character

instead of Parent

2 comments

0

pppppp

0

But its not for a Player Can it work with Non-Playable Character?

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