Why is this simple code not working?

I’m making a topbar button that kills you when you click it. But it’s not working. Here’s the code:

local Icon = require(game:GetService("ReplicatedStorage").Icon)
local Sound = game.StarterGui.Sounds.BackgroundMusic
local icon = Icon.new()
	
local Humanoid = game.Players.LocalPlayer.Character.Humanoid

:setLabel("Respawn", "selected")
:setLabel("Respawn", "deselected")
:setLabel("Reset Your Character!", "hovering")
	
:setProperty("deselectWhenOtherIconSelected", false)

icon:setMid()

icon.selected:Connect(function()
	
	icon:deselect()
	Humanoid.Health = 0
	
end)

icon.deselected:Connect(function()
	
	Humanoid.Health = 0
	
end)

why is it not working? can you help me fix it? ill give you a solution

3 Likes
:setLabel("Respawn", "selected")
:setLabel("Respawn", "deselected")
:setLabel("Reset Your Character!", "hovering")
	
:setProperty("deselectWhenOtherIconSelected", false)

I think these methods arent being used on anything so its throwing an error

That’s for the label

thats for when another icon is selected it doesnt deselect the icon so that has nothing to do with it

1 Like

You should access the Character’s Humanoid through the function instead of calling it at the beginning of the script.

1 Like
local player = game.Players.LocalPlayer

icon:bindEvent("selected", function(self)
	local char = player.Character or player.CharacterAdded:Wait() 

local hum = char:FindFirstChild("Humanoid").Health = 0

icon:deselect() -- idk if this works but you put it..... so..
end)

I put the humanoid variable inside the function because if they die, then the humanoid will become nil, so you put it inside the function so it “refinds it” every time you press it.

1 Like

no it doesnt work char char char

1 Like

i tried using a function but it didnt work

1 Like

imma goofball but do the events fire when you select/deselect the icon?
try adding a print statement in both of the events and seeing if they print

1 Like

nothing is printing

also heres the code i am using

local Icon = require(game:GetService("ReplicatedStorage").Icon)
local Sound = game.StarterGui.Sounds.BackgroundMusic
local icon = Icon.new()
	
local Humanoid = game.Players.LocalPlayer.Character.Humanoid

:setLabel("Respawn", "selected")
:setLabel("Respawn", "deselected")
:setLabel("Reset Your Character!", "hovering")
	
:setProperty("deselectWhenOtherIconSelected", false)

icon:setMid()

icon.selected:Connect(function()
	
	icon:deselect()
	Humanoid.Health = 0
	print("hi")
	
end)

icon.deselected:Connect(function()
	
	Humanoid.Health = 0
	print("hi")
	
end)
1 Like

Are there any docs for this Icon module you’re using?

It does seem like the events aren’t firing based off of the fact that nothing’s printing. Perhaps either the events aren’t real or that’s not the proper way to use .selected/.deselected? (there could be more possibilities but im just sayin)

image
Image taken from TopbarPlus module

Assuming you’re using the TopbarPlus module, this might be the proper way to use selected and deselected

local Icon = require(game:GetService("ReplicatedStorage").Icon)
local Sound = game.StarterGui.Sounds.BackgroundMusic
local icon = Icon.new()

:setLabel("Respawn", "selected")
:setLabel("Respawn", "deselected")
:setLabel("Reset Your Character!", "hovering")
	
:setProperty("deselectWhenOtherIconSelected", false)

icon:setMid()

icon:bindEvent("selected", function(icon)
        local Humanoid = game.Players.LocalPlayer.Character.Humanoid  
        icon:deselect()
        Humanoid.Health = 0
        print("hi")
end)


icon:bindEvent("deselected", function(icon)
        local Humanoid = game.Players.LocalPlayer.Character.Humanoid  
        Humanoid.Health = 0
        print("hi")
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.