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?
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
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.
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)