Show button when npc is hovered

  1. What do you want to achieve? Keep it simple and clear!
    I want to achieve a button thats shows when npc is hovered.

I have made a script that make the button show when the mouse is hovering the npc but when the mouse goes of the character then it wont dissapear.

Here is my script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local CollectionService = game:GetService("CollectionService")

mouse.Move:Connect(function()
	if CollectionService:HasTag(mouse.Target, "ShowNPCButton") then
		mouse.Target.Parent.Torso.ButtonBillBoardGui.TextButton.Visible = true
		mouse.Target.Parent.Torso.TextBillboardGui.TextLabel.Visible = true
		return
	elseif CollectionService:HasTag(mouse.Target.Parent, "ShowNPCButton") then
		mouse.Target.Parent.Torso.ButtonBillboardGui.TextButton.Visible = true
		mouse.Target.Parent.Torso.TextBillboardGui.TextLabel.Visible = true
		return
	end
end)

1 Like

Try this:

--//Services
local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()

--//Tables
local ActiveGuis = {}

--//Functions
RunService.RenderStepped:Connect(function()
	local mouseTarget = Mouse.Target
	
	if CollectionService:HasTag(mouseTarget, "ShowNPCButton") or CollectionService:HasTag(mouseTarget.Parent, "ShowNPCButton") then
		local Torso = mouseTarget.Parent.Torso
		Torso.ButtonBillBoardGui.TextButton.Visible = true
		Torso.TextBillboardGui.TextLabel.Visible = true
		
		table.insert(ActiveGuis, Torso.ButtonBillBoardGui.TextButton)
		table.insert(ActiveGuis, Torso.TextBillboardGui.TextLabel)
	elseif #ActiveGuis > 0 then
		for i, gui in ipairs(ActiveGuis) do
			gui.Visible = false
		end
		
		table.clear(ActiveGuis)
	end
end)
2 Likes

Thank you so much i really appreciate it

1 Like

No problem. If you have any more questions, feel free to ask.

2 Likes

Actually i have a question.

my xp on npc kill script dosen’t work for some reason.

here it is if you could take a look

local humanoid = script.Parent.Humanoid
function Died()
	local tag = humanoid:FindFirstChild("creator")
	if tag ~= nil then
		if tag.Value ~= nil then
			local leaderstats = tag.Value:FindFirstChild("PlayerStats")
			if leaderstats ~= nil then
				leaderstats.XP.Value = leaderstats.XP.Value + math.random(1.2, 4.7)
				wait(0.1)
				script:Remove()
			end
		end
	end
end
humanoid.Died:Connect(Died)
1 Like

When a player hits the NPC, does it add a creator tag?

where would i check if it has added it? i forgot where it was

Try CTRL Shift F and type in creator. Paste the script that pops up.

local humanoid = script.Parent.Humanoid
function Died()
	local tag = humanoid:FindFirstChild("creator")
	if tag ~= nil then
		if tag.Value ~= nil then
			local leaderstats = tag.Value:FindFirstChild("PlayerStats")
			if leaderstats ~= nil then
				leaderstats.XP.Value = leaderstats.XP.Value + math.random(1.2, 4.7)
				wait(0.1)
				script:Remove()
			end
		end
	end
end
humanoid.Died:Connect(Died)

this is what was at the top

Is there any other scripts? If there isn’t you need to make a script that creates the creator tag when the player hits the npc.

there wasn’t but how will i be able to create the creator tag script?

Send the script you use to damage the npc.

here:

local Character = script.Parent --rbxassetid://10912047827 left -- right rbxassetid://10912044667
local Player = game.Players.LocalPlayer
local Humanoid = Character:WaitForChild("Humanoid")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Mouse = game.Players.LocalPlayer:GetMouse()

local LeftPunch = script:WaitForChild('LeftPunch')
local RightPunch = script:WaitForChild('RightPunch')

local RightArm = Character:WaitForChild("Right Arm")
local LeftArm = Character:WaitForChild("Left Arm")

local Punched = false
local Side = false
local Dmg = false

local DamageAmount = Player:WaitForChild("PlayerStats"):WaitForChild("PlayerDamage")


Mouse.Button1Down:Connect(function()
	if Punched == false and Side == false then
		Side = true
		Punched = true
		
		
		Dmg = true
		
		
		local Animaton = Humanoid:LoadAnimation(LeftPunch)
		Animaton:Play()
		
		script.Sound:Play()
		
		LeftArm.Touched:Connect(function(Hit)
			local Human = Hit.Parent:FindFirstChild("Humanoid")
			if Human and Dmg == true then
				script.Hit:Play()
				local hitAnim = Human:LoadAnimation(script.LeftHit)
				hitAnim:Play()
				Human:TakeDamage(DamageAmount.Value)
				ReplicatedStorage.Punch:FireServer(Hit)
				Dmg = false
				
			end
			
			
			
			
		end)
		
		Animaton.Stopped:Connect(function()
			
			wait(0.1)
			Punched = false
			
		end)
		
		
	elseif Punched == false and Side == true then
		Side = false
		Punched = true
		
		Dmg = true


		local Animaton = Humanoid:LoadAnimation(RightPunch)
		Animaton:Play()

		script.Sound:Play()

		RightArm.Touched:Connect(function(Hit)
			local Human = Hit.Parent:FindFirstChild("Humanoid")
			if Human and Dmg == true then
				script.Hit:Play()
				local hitAnim = Human:LoadAnimation(script.RightHit)
				hitAnim:Play()
				Human:TakeDamage(DamageAmount.Value)
				ReplicatedStorage.Punch:FireServer(Hit)
				Dmg = false

			end




		end)

		Animaton.Stopped:Connect(function()

			wait(0.1)
			Punched = false

		end)
		
	end
	
end)

Change your script to this:

local Character = script.Parent --rbxassetid://10912047827 left -- right rbxassetid://10912044667
local Humanoid = Character:WaitForChild("Humanoid")

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local LeftPunch = script:WaitForChild('LeftPunch')
local RightPunch = script:WaitForChild('RightPunch')

local RightArm = Character:WaitForChild("Right Arm")
local LeftArm = Character:WaitForChild("Left Arm")

local Punched = false
local Side = false
local Dmg = false

local DamageAmount = Player:WaitForChild("PlayerStats"):WaitForChild("PlayerDamage")

local function Damage(humanoid, damage)
	local objectValue = Instance.new("ObjectValue")
	objectValue.Name = "creator"
	objectValue.Value = Player
	objectValue.Parent = humanoid
	
	task.delay(3, function()
		if objectValue:IsDescendantOf(workspace) then
			objectValue:Destroy()
		end
	end)
	
	humanoid:TakeDamage(damage)
end

Mouse.Button1Down:Connect(function()
	if not Punched and not Side then
		Side = true
		Punched = true

		Dmg = true
		
		local Animaton = Humanoid:LoadAnimation(LeftPunch)
		Animaton:Play()

		script.Sound:Play()

		LeftArm.Touched:Connect(function(Hit)
			local Human = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
			
			if Human and Dmg then
				Dmg = false

				script.Hit:Play()
				
				local hitAnim = Human:LoadAnimation(script.LeftHit)
				hitAnim:Play()
			
				Damage(Human, DamageAmount.Value)
				ReplicatedStorage.Punch:FireServer(Hit)
			end
		end)

		Animaton.Stopped:Connect(function()
			task.wait(0.1)
			
			Punched = false
		end)

	elseif not Punched and Side then
		Side = false
		Punched = true

		Dmg = true

		local Animaton = Humanoid:LoadAnimation(RightPunch)
		Animaton:Play()

		script.Sound:Play()

		RightArm.Touched:Connect(function(Hit)
			local Human = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
			
			if Human and Dmg then
				Dmg = false

				script.Hit:Play()
				
				local hitAnim = Human:LoadAnimation(script.RightHit)
				hitAnim:Play()
				
				Damage(Human, DamageAmount.Value)
				ReplicatedStorage.Punch:FireServer(Hit)
			end
		end)

		Animaton.Stopped:Connect(function()
			task.wait(0.1)
			
			Punched = false
		end)
	end
end)

i still don’t get xp but it has found another script in CTRL SHIFT F

Wait I just realized it won’t work because you’re damaging it on a local script. Learn about remote events and try to use them for this. I need to go right now so you can learn about it while I’m gone.

alr but i already have a punch remote event in replicated storage

Send me your code for your remote event.

Line: 61, 94

i have highlighted them

local Character = script.Parent --rbxassetid://10912047827 left -- right rbxassetid://10912044667
local Humanoid = Character:WaitForChild("Humanoid")

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local LeftPunch = script:WaitForChild('LeftPunch')
local RightPunch = script:WaitForChild('RightPunch')

local RightArm = Character:WaitForChild("Right Arm")
local LeftArm = Character:WaitForChild("Left Arm")

local Punched = false
local Side = false
local Dmg = false

local DamageAmount = Player:WaitForChild("PlayerStats"):WaitForChild("PlayerDamage")

local function Damage(humanoid, damage)
	local objectValue = Instance.new("ObjectValue")
	objectValue.Name = "creator"
	objectValue.Value = Player
	objectValue.Parent = humanoid

	task.delay(3, function()
		if objectValue:IsDescendantOf(workspace) then
			objectValue:Destroy()
		end
	end)

	humanoid:TakeDamage(damage)
end

Mouse.Button1Down:Connect(function()
	if not Punched and not Side then
		Side = true
		Punched = true

		Dmg = true

		local Animaton = Humanoid:LoadAnimation(LeftPunch)
		Animaton:Play()

		script.Sound:Play()

		LeftArm.Touched:Connect(function(Hit)
			local Human = Hit.Parent:FindFirstChildWhichIsA("Humanoid")

			if Human and Dmg then
				Dmg = false

				script.Hit:Play()

				local hitAnim = Human:LoadAnimation(script.LeftHit)
				hitAnim:Play()

				Damage(Human, DamageAmount.Value)
				**ReplicatedStorage.Punch:FireServer(Hit)**
			end
		end)

		Animaton.Stopped:Connect(function()
			task.wait(0.1)

			Punched = false
		end)

	elseif not Punched and Side then
		Side = false
		Punched = true

		Dmg = true

		local Animaton = Humanoid:LoadAnimation(RightPunch)
		Animaton:Play()

		script.Sound:Play()

		RightArm.Touched:Connect(function(Hit)
			local Human = Hit.Parent:FindFirstChildWhichIsA("Humanoid")

			if Human and Dmg then
				Dmg = false

				script.Hit:Play()

				local hitAnim = Human:LoadAnimation(script.RightHit)
				hitAnim:Play()

				Damage(Human, DamageAmount.Value)
				**ReplicatedStorage.Punch:FireServer(Hit)**
			end
		end)

		Animaton.Stopped:Connect(function()
			task.wait(0.1)

			Punched = false
		end)
	end
end)

No, send your code for your remote event. Like send the server side code for your remote event.