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

Disable jumping on connect?

I need help. I made a script that plays a sound when you enter an object and it ends the sound ontouch end. Since touch end is glitchy the sound starts over when you jump. I need a fix to disable jumping onconnect with a part but I don't know how. Can someone help? This is what I have so far.

debounce = false

script.Parent.Touched:connect(
    function(hit)
        if debounce == false then
            debounce = true
            if (hit.Parent:FindFirstChild("Humanoid") ~= nil) then
                local player = game.Players:GetPlayerFromCharacter(hit.Parent)
                local sound = script.Parent.Sound:Clone()
                sound.Parent = player.PlayerGui
                sound:Play()
                sound.Ended:wait()
                debounce = false          
			end
        end
    end
)
script.Parent.TouchEnded:Connect(
    function(hit)
            debounce = false
            local playergui = game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui
            if playergui.Sound then
                local sound = playergui.Sound
                sound:Destroy()
		end
    end
)

2 answers

✓ Accepted answer

You could use Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false).

When set to false the player is unable to jump, when true they have the ability to jump.

debounce = false

script.Parent.Touched:connect(
    function(hit)
        if debounce == false then
            debounce = true
            if (hit.Parent:FindFirstChild("Humanoid") ~= nil) then
                local Humanoid = hit.Parent.Humanoid
                Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false) -- makes player unable to jump
                local player = game.Players:GetPlayerFromCharacter(hit.Parent)
                local sound = script.Parent.Sound:Clone()
                sound.Parent = player.PlayerGui
                sound:Play()
                sound.Ended:wait()
                debounce = false
            end
        end
    end
)
script.Parent.TouchEnded:Connect(
    function(hit)
        debounce = false
        local Humanoid = hit.Parent.Humanoid
        Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true) -- makes player able to jump
        local playergui = game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui
        if playergui.Sound then
            local sound = playergui.Sound
            sound:Destroy()
        end
    end
)


More information: https://developer.roblox.com/en-us/api-reference/function/Humanoid/SetStateEnabled

You do not need to disable jumping. You do not need crappy TouchEnded.. What you really need is a reliable way to tell when player has left the area, and when to "reset" the trigger. Best way to achieve that is combination of both Touched and magnitude.

local register = {}

script.Parent.Touched:connect(function(hit)

	local root = hit.Parent:FindFirstChild("HumanoidRootPart")
	if not root then
		return -- touch by non-humanoid
	end

	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not player then
		return --could it be NPC?
	end

	if register[player] then
		return -- this player is already registered
	end
	
	register[player] = true
	local sound = script.Parent.Sound:Clone()
	sound.Parent = player.PlayerGui
	sound.Looped = true --auto sound looping
	sound:Play()

	--keep player in register until he/she leaves
	--i will put leave distance at 10 studs, but feel free to adjust
	--it should be slightly larger than the trigger part radius
	while hit.Parent and (root.Position - script.Parent.Position).magnitude < 10 do
		wait(1)
	end

	--player has left, destroy sound and remove from register
	sound:Destroy()
	register[player] = nil	

end)

If you do not like this solution (why?) then use

local humanoid = hit.Parent.Humanoid
humanoid.JumpPower = 0

and in touch ended

local humanoid = hit.Parent.Humanoid
humanoid.JumpPower = 50 --default

2 comments

0

didnt work

0

sorry

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