i have opened previous topic about my problem and i marked it for solved but for real it didn’t solve my problem…
i want to make when player touches the part, then the part will go slowly down and when player ends the touch it will go back to original position. for some reason my part is glitching up and down and i don’t know what to do please help me.
code:
local MainGame = require(game:GetService("ServerScriptService"):FindFirstChild("MainGame"))
local Debounce = false
local Stages = game.Workspace:FindFirstChild("Stages")
local StageNum = Stages:FindFirstChild("Stage1") -- Stage 1
local Obstacle = StageNum:FindFirstChild("RedBridge") -- Red Bridge
local ButtonA = Obstacle:FindFirstChild("ButtonA")
local PartToMove = ButtonA.Button
local DefaultY = PartToMove.Position.Y
local PartToCollide = Obstacle.Bridge
PartToCollide.CanCollide = false
PartToCollide.Transparency = 0.5
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if Debounce == false then
MainGame.MoveButton(PartToMove,DefaultY - 0.25)
--if PartToCollide.CanCollide == false then
PartToCollide.CanCollide = true
PartToCollide.Transparency = 0
--end
Debounce = true
end
end
end)
script.Parent.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if Debounce == true then
MainGame.MoveButton(PartToMove,DefaultY)
--if PartToCollide.CanCollide == true then
PartToCollide.CanCollide = false
PartToCollide.Transparency = 0.5
--end
Debounce = false
end
end
end)
module script code:
local module = {}
module.MoveButton = function(PartToMove,DefaultY)
local tweenInfo = TweenInfo.new(0.25, Enum.EasingStyle.Linear)
local targetPosition = Vector3.new(PartToMove.Position.X, DefaultY, PartToMove.Position.Z)
local tween = game:GetService("TweenService"):Create(PartToMove, tweenInfo, {Position = targetPosition})
tween:Play()
end
return module
I think this is an issue with .TouchEnded since it’s not a very reliable event, could you try printing stuff like “Touch has ended” in the event to see if the TouchEnded event is getting fired too early / randomly?
You have a Debounce, but you have no task.wait(number) in there.
Touched and TouchEnded will both keep continuously firing when any part of your avatar touches the item. If you put a print("touched began") line in there you’ll see it going up very quickly.
You should try putting a number like .2 or similar in the task.wait().
Also inside the Touched function put the TouchEnded section, so it only fires the one time and put the debounce = lines before and after the entire group instead so it only fires once when hit, the debounce turns off the Touched function, and is only reset after the TouchEnded fires.
where do i need place task.wait() in which line?
and does you mean its need to look like that:
code:
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if Debounce == false then
MainGame.MoveButton(PartToMove,DefaultY - 0.25)
--if PartToCollide.CanCollide == false then
PartToCollide.CanCollide = true
PartToCollide.Transparency = 0
--end
Debounce = true
script.Parent.TouchEnded:Connect(function(hit)
print("Touch Ended")
if hit.Parent:FindFirstChild("Humanoid") then
if Debounce == true then
MainGame.MoveButton(PartToMove,DefaultY)
--if PartToCollide.CanCollide == true then
PartToCollide.CanCollide = false
PartToCollide.Transparency = 0.5
--end
Debounce = false
end
end
end)
end
end
end)
I might be completely wrong about having the TouchEnded inside the Touched function.
I can’t try it out in Studio, but maybe like this
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if Debounce == false then
print("Touch Ended") -- to show when touch starts
MainGame.MoveButton(PartToMove,DefaultY - 0.25)
--if PartToCollide.CanCollide == false then
PartToCollide.CanCollide = true
PartToCollide.Transparency = 0
--end
script.Parent.TouchEnded:Connect(function(hit)
print("Touch Ended") -- you can compare Touched and TouchEnded functions
if hit.Parent:FindFirstChild("Humanoid") then
MainGame.MoveButton(PartToMove,DefaultY)
--if PartToCollide.CanCollide == true then
PartToCollide.CanCollide = false
PartToCollide.Transparency = 0.5
--end
end
end
end)
end
task.wait(1) -- or whatever time works
Debounce = false -- may only need one to keep Touched event from firing over and over while these functions are true?
end
end)
it helped it but still jitter down and up. i am still sure its the problem with the touch event or somewhere in this part of the code, but can’t realize it i just want it not jitter up and down. when player touches it once it will go down and make other part’s collision true and when player steps of this button it will go back up and make collision false
building on @Scottifly 's code suggestion, this might work better.
Once a touchended event has been connected it will still fire once anything ends its touch, even if it doesn’t fire when touched.
local Debounce = false
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and Debounce == false then
Debounce = true
print("Touched debounce is", Debounce) -- to show when touch starts
--MainGame.MoveButton(PartToMove,DefaultY - 0.25)
--if PartToCollide.CanCollide == false then
--PartToCollide.CanCollide = true
--PartToCollide.Transparency = 0
--end
local touching = true --Added a touching debounce, else touchended fires multiple times.
script.Parent.TouchEnded:Connect(function(hit)
if touching == true then
touching = false
print("Touch Ended, touching is", touching)
if hit.Parent:FindFirstChild("Humanoid") then
--MainGame.MoveButton(PartToMove,DefaultY)
--if PartToCollide.CanCollide == true then
--PartToCollide.CanCollide = false
--PartToCollide.Transparency = 0.5
--end
end
end
task.wait(0.5)
end)
task.wait(1)
Debounce = false
end
end)
its even worse now, as you can see its not moving down and up sometimes. if there is other way to make what my goal is then i appreciate if someone can explain to me how can i do it.
Just checking you removed the comment marks? I only edited the code provided to get it to print without causing debounce errors, I haven’t attempted to replicate the entire setup but I can’t see any errors with the module running the Tween.
But what are the print statements in the output window showing?
When you test and touch the Part, how many Touched events are showing. If it’s only 1 then it should be fine, but if you have something like 22 or 40 Touched events then we’ll need to stop that from happening using the debounce.
its printing once and not spaming but some where in the code there is problem its jitter up and down i don’t know from what it happens… maybe its problem with the part movment in the module script or i don’t know.
Put a print in the module script to see if that’s actually running. If if does then it may have an issue with your tween section of code.
You seem to set your DefaultY variable at the beginning of the first script, but when you call the tween you set the position of the Part to DefaultY. If the y position isn’t changed then the Part won’t move up and down.
the part is moving up and down in all cases, if it had error there it had to error me but there is no error and the part is moving up and down but i think maybe it makes the problem while it moves down or up i mean the touch section glitches
i will try to add print but again i don’t think its there the problem. if you had to make the button go down when player stands on it how would you make it?
local TweenService = game:GetService("TweenService")
local part = script.Parent
local originalPosition = part.Position
local downPosition = originalPosition - Vector3.new(0, 1, 0)
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local isTouched = false
local function onTouch()
if not isTouched then
isTouched = true
local tweenDown = TweenService:Create(part, tweenInfo, {Position = downPosition})
tweenDown:Play()
end
end
local function onTouchEnded()
if isTouched then
isTouched = false
local tweenUp = TweenService:Create(part, tweenInfo, {Position = originalPosition})
tweenUp:Play()
end
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)