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

Hover Bricks Broken When Reset?

This is a hover brick that will show an image GUI whenever someone hover their mouse over the brick. But for some reason, when I reset my character it doesn't work anymore. The GUI doesn't show up when I hovered my mouse over the brick. How come? Any ideas why this is happening? Everything works just fine except when I reset my character.

Here's a gif to see what I'm talking about:

https://gyazo.com/67f5241678d1938b37d058a95af210cd

Here's an images of my layout, please check them:

https://gyazo.com/e82c9fbc878d22ffcddcbaa437ceb01d

Here's the image of the script (This is a regular script):

https://gyazo.com/c809291d05fc909bb5e324efc2645f1e

game.Players.PlayerAdded:connect(function(newPlayer)
    local plr = game.Players:FindFirstChild(newPlayer.Name)
    if plr then
        local gui = script:WaitForChild("ScreenGui"):Clone() -- if you change the name of "ScreenGui" change it in this script too.
        gui.Parent = plr.PlayerGui
    end
end)

Here's another script (This is a local script):

https://gyazo.com/179765711ef2a1e8af76c9a9a9b90c9b

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local distance = 20 -- Set to whatever you want the distance to be

function onHover(plr)
    local char = plr.Character
    if char then        
        local torsoDistance = char.Torso.Position.magnitude
        if torsoDistance < distance then
            print(char.Torso.Position.magnitude)
            return true 
        end
    end
    return false
end

while wait() do
    if Mouse.Target ~= nil and Mouse.Target.Name == 'HoverBrick' then
        local isCloseEnough = onHover(Player)
        if isCloseEnough then
            script.Parent.Frame.ImageLabel.Image = Mouse.Target.NewTexture.Texture
            script.Parent.Frame.Visible = true  
        else
            script.Parent.Frame.Visible = false         
        end

    else
        script.Parent.Frame.Visible = false
    end
end

If you need to examine the model, then click this link and it will take you straight to the model that I need help on:

https://www.roblox.com/Broken-When-Reset-item?id=483308062

Many thanks, my friends!

1 answer

✓ Accepted answer

The problem is that when the character resets, the contents of the PlayerGui are removed. Therefore, you need to re-add them every time the character is created, rather than just when the player is. Here is the corrected script (the normal one) with some unnecessary code removed:

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAdded:connect(function()
		local gui = script:WaitForChild("ScreenGui"):Clone() -- if you change the name of "ScreenGui" change it in this script too.
		gui.Parent = plr.PlayerGui
	end)
end)

Additionally, I noticed your GUI uses offset. It is better to use scale as it works on all screen sizes.

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