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

Any Ideas Why This Won't Work When I Change My Character Position?

This is a hover brick script which make the "Decal" show up when someone hover their mouse over to the brick. But there's one problem though, whenever I move my character position, the hover brick doesn't work when I hover my mouse over the brick. Any ideas why this is happening?

Also, here is my layout: https://gyazo.com/4b634bcdc1a17d447ff033ba4005fca6

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

Thank you for your time and consideration.

1 comment

0

I'm assuming this is a local script, which it should be, and I'm assuming this is in a part in the workspace. Local scripts don't work in workspace.

1 answer

I think your onhover function is messed up.

if char then        
        local torsoDistance = char.Torso.Position.magnitude
        if torsoDistance < distance then
            print(char.Torso.Position.magnitude)
            return true 
        end
    end

should be:

-- Define hoverbrick part as local part = part.Position
if char then        
        local torsoDistance = char.Torso.Position
        if (torsoDistance -  part).magnitude < distance then
            print(char.Torso.Position.magnitude)
            return true 
        end
    end

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