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)
--//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)
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)
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)
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)
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)
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.
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)