This script I made that changes the player's walk animation when inside a Region doesn't work, fixes?

In my game, I have a part where it makes a Region3 based off of where the part is and it’s size. If you enter the region, it changes the variable Walk’s value, changing the walk animation. This is used for a stairs system. I have a script inside ServerScriptService called Script, another script inside ServerScriptService called Stairs, a part called StairsHitbox in Workspace and a StringValue called Walk in a folder called WalkFolder inside of Workspace. Here is Script:

local Players = game:GetService("Players")
 
local function onCharacterAdded(character)
    local humanoid = character:WaitForChild("Humanoid")
 
    for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
        playingTracks:Stop(0)
    end
 
local animateScript = character:WaitForChild("Animate")
    animateScript.run.RunAnim.AnimationId = game.Workspace.WalkFolder.Walk.Value
    animateScript.walk.WalkAnim.AnimationId = game.Workspace.WalkFolder.Walk.Value
end
 
local function onPlayerAdded(player)
    player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)

Here is Stairs:

local StairsHitbox = game.Workspace.StairsHitbox
local pos1 = StairsHitbox.Position - (StairsHitbox.Size / 2)
local pos2 = StairsHitbox.Position + (StairsHitbox.Size / 2)
local region = Region3.new(pos1, pos2)

while true do
    wait()
    local partsInRegion = workspace:FindPartsInRegion3(region, nil, 100000000)
    for i, part in pairs(partsInRegion) do
        if part.Parent:FindFirstChild("Humanoid") ~= nil then
            print("Player found in the stairs region, name is ".. part.Parent.Name)
            local char = part.Parent
            game.Workspace.WalkFolder.Walk.Value = "rbxassetid://4901864733"
        else
            game.Workspace.WalkFolder.Walk.Value = "rbxassetid://4897863146"
        end
    end
end

Why is this not changing the player’s animation while they are in the Region? It prints it, but it doesn’t change the value of Walk, with no errors.

(It’s probably a stupid mistake, I’m just new to scripting and still learning(

1 Like

You can use Region3 for it, and detect if player is on that region and verify the velocity of humanoid root part, if the velocity is higher than 0 its because player is walking upstairs. If the velocity is lower than 0 its because player is walking downstairs. And if the velocity is 0 its because player is not walking.

1 Like

Not what I was looking for, but thanks for the help. Using up and down instead of left and right is smart though, thank you :slight_smile:

Can you elaborate on what you are looking for? Using a touched event for something such as this won’t have it working smoothly. Region3 isn’t a bad way to go, but I would probably opt for using magnitude. Fenix also has a good way of checking if they are walking up or down. Putting these two together should help you solve your problem.

1 Like

If the are touching it it mean they are on the stairs. If they are going up or down the Y axis I guess that works better then using the X axis, let me edit my post real quick, thank you :slight_smile:

Edit: Done! Also, what do you mean won’t run smoothly?

Checking if the player’s y axis is increasing is far harder to do than check to see if the players humanoid root part velocity is positive or negative.

When I say run smoothly it’s hard to describe because to be honest I don’t know exactly what is happening. Sometimes when I’m inside the part but moving around it detects I’ve touched it and other times it doesn’t. Sometimes when you walk out of a part, the Touched event fires after the TouchEnded event fires. I guess the term unreliable is probably better.

1 Like

I edited the post to make it way easier, as it should work as well. If you want to point me in the right direction I might be able to do this myself?

I’m getting close to getting it right…

I think I finally got it right myself, I just need some further help. Let me edit this post entirely…
Edit: Alright I changed the post, would you happen to know what’s wrong here? (Also btw I asked a mod originally and they them self said to post here, guess it was a mistake)

game.Workspace.WalkFolder.Walk:GetPropertyChangedSignal("Value"):Connect(function(Animation) -- Updates to the new values
local animateScript = character:WaitForChild("Animate")
    animateScript.run.RunAnim.AnimationId = Animation
    animateScript.walk.WalkAnim.AnimationId = Animation
end
end)

This should fix!

1 Like

Thanks! I’ll try it and see how it goes :slight_smile:

local Players = game:GetService("Players")
 
local function onCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid")
 
	for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
		playingTracks:Stop(0)
	end
 
game.Workspace.WalkFolder.Walk:GetPropertyChangedSignal("Value"):Connect(function(Animation) -- Updates to the new values
local animateScript = character:WaitForChild("Animate")
animateScript.run.RunAnim.AnimationId = Animation
animateScript.walk.WalkAnim.AnimationId = Animation
end
 
local function onPlayerAdded(player)
	player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)

This is what script looks like now. Is this correct?

Yes, it should be like that. (30 Chars)

1 Like

Seems to error out, here’s the error: ServerScriptService.Script:16: Expected ‘)’ (to close ‘(’ at line 10), got ‘local’

Oh you missed the “end)” at line 11 make sure you use it like this:

game.Workspace.WalkFolder.Walk:GetPropertyChangedSignal("Value"):Connect(function(Animation) -- Updates to the new values
local animateScript = character:WaitForChild("Animate")
    animateScript.run.RunAnim.AnimationId = Animation
    animateScript.walk.WalkAnim.AnimationId = Animation
end
end) -- This end over here
1 Like

Another error? ServerScriptService.Script:15: Expected ‘)’ (to close ‘(’ at line 10), got ‘end’

Also thanks for the help, highly appreciate it

Can you show me the lines where the error occured?

1 Like
  game.Workspace.WalkFolder.Walk:GetPropertyChangedSignal("Value"):Connect(function(Animation) -- Updates to the new values
    local animateScript = character:WaitForChild("Animate")
        animateScript.run.RunAnim.AnimationId = Animation
        animateScript.walk.WalkAnim.AnimationId = Animation
    end
    end)

Lines 10-15

1 Like
game.Workspace.WalkFolder.Walk:GetPropertyChangedSignal("Value"):Connect(function()
  local animateScript = character:WaitForChild("Animate")
      animateScript.run.RunAnim.AnimationId = game.Workspace.WalkFolder.Walk.Value
      animateScript.walk.WalkAnim.AnimationId = game.Workspace.WalkFolder.Walk.Value
  end)
  end

Ah, I forgot to put the end) before the other, since its linked to onCharacterAdded function, should work now.

Sorry, it’s 4:40 and I barely can keep up :tired_face:

1 Like

Oh sorry man, if you want to work on it later that’s alright.