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