How would I go trying to make a button which scales when a player stand on it?

Hello!
I am quite new to coding and would like some help.
I have made some buttons for a tycoon game and would like to make them get smaller when they get stepped on and go to the normal size when the player steps off them.

I know how to make the button scale in the script but how would I detect if a player steps on the button?

Thanks for any soloutions! :grinning:

I think the button is a part, if you want to detect when a player touches it, use part.Touched and to detect when he steps-of it , use part.TouchEnded

k thanks i will try this (random thing to make it so i have correct words)

1 Like

You could bind a magnitude check loop to the touched event. The button would run a loop until the player leaves the magnitude.
TouchEnded can be unreliable :thinking:

He said he is still new, magnitude wont be a good suggestion, he could use Zone+ since its more beginner-friendly.

uhh how would i do this? im kinda new

btw this is the first script i made:

local button = script.Parent
if button.Touched then
	button.Size = Vector3.new(5.5,1.049,5.5)
end

if button.TouchEnded then
	button.Size = Vector3.new(6.5,1.049,6.5)
end

but when i step on the button it doesnt do anything and there are no errors.
why is this?

local function MagnitudeCheck(Character)
local magn = (Character.HumanoidRootPart.Position - Button.Position).magnitude
wait(0.5)
if magn < 8 then
MagnitudeCheck()
else
--Tween the size back to normal
end
end

script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local Character = hit.Parent
MagnitudeCheck(Character)
end
end)

Iā€™m on phone writing this so this is it just in a nutshell. It will need debouncing to make it not happen on each hit per character for an example.

By the wat, I suggest you take a look at TweenService for smoother scaling of things. :cowboy_hat_face:

this is my code:

local Button = script.Parent

local function MagnitudeCheck(Character)
	local magn = (Character.HumanoidRootPart.Position - Button.Position).magnitude
	wait(0.5)
	if magn < 8 then
		MagnitudeCheck()
	else
		--Tween the size back to normal
	end
end

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Character = hit.Parent
		MagnitudeCheck(Character)
	end
end)

it says at line 4, attempt to index nil with HumanoidRootPart

local Button = script.Parent

local function MagnitudeCheck(HRP)
	local magn = (HRP.Position - Button.Position).magnitude
	wait(0.5)
	if magn < 8 then
		MagnitudeCheck(HRP)
	else
		--Tween the size back to normal
	end
end

script.Parent.Touched:connect(function(hit)
local HRP = hit.Parent:FindFirstChild("HumanoidRootPart")
        if HRP then
		local Character = hit.Parent
		MagnitudeCheck(HRP)
	end
end)

Try this :thinking:

sorry for late response but when i step on the button nothing happens