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

Why Won't The Hover Brick Work When Move?

This is a hover brick that will show an image GUI whenever someone hover their mouse over the brick. But for some reason it doesn't work. If I move my character to a different position and hover the mouse over the brick, the image GUI isn't showing up. How come? Any ideas why this is happening?

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

https://gyazo.com/b9a9ead14dfd6a69d534bc5a0845022f

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

https://gyazo.com/9129737b7eb3de37e02fee10d255e511

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):

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 mode, then click this link and it will take you straight to the model that I need help on:

https://www.roblox.com/Broken-item?id=462522261

Many thanks, my friend!

1 answer

✓ Accepted answer

I think I found the problem

You're using Magnitude wrong.

Magnitude returns the distance in studs from one Vector3 to/from another. Example,

local part1 = workspace.Part1
local part2 = workspace.Part2

local Distance = (part1.Position-part2.Position).Magnitude
print(Distance)

If however, you only use one Position, like you're doing, the distance would be dependent on where the player is.

Fix,

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

function onHover(plr, targ)
    local char = plr.Character
    if char then        
        local torsoDistance = (char.Torso.Position-targ.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, Mouse.Target)
        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

That should fix your problem. It worked for me anyways. Here's the link to the fixed model if you need it.

Good Luck!

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