I made this script for a roblox animation for idle, which is a Script in ServerScriptService. Basically, I want it to change the animation when a stringvalue named HandedValue in ReplicatedStorage’s value is right or left. So if HandedValue = right, play the right animation. It works, but when I change the value in-game, it doesn’t change the animation. I want it to change the animation when the value changes in game. Help is appreciated. Thanks
Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local handedValue = ReplicatedStorage:WaitForChild("HandedValue")
local function updateAnimation(Character)
local animationId
if handedValue.Value == "right" then
animationId = "rbxassetid://13795555699"
elseif handedValue.Value == "left" then
animationId = "rbxassetid://13833716962"
else
animationId = "rbxassetid://13795555699"
end
Character.Animate.idle.Animation1.AnimationId = animationId
end
local function handlePlayerAdded(Player)
Player.CharacterAdded:Connect(function(Character)
updateAnimation(Character)
end)
end
game.Players.PlayerAdded:Connect(handlePlayerAdded)
for _, Player in ipairs(game.Players:GetPlayers()) do
if Player.Character then
updateAnimation(Player.Character)
end
end
handedValue.Changed:Connect(function()
for _, Player in ipairs(game.Players:GetPlayers()) do
if Player.Character then
updateAnimation(Player.Character)
end
end
end)
It doesnt change mid-game, as the script is only ran once (when the character spawns in)
To fix this, you can use :GetPropertyChangedSignal()
It would be something like this. Sorry for the bad formatting.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local handedValue = ReplicatedStorage:WaitForChild("HandedValue")
local function updateAnimation(Character)
local animationId
if handedValue.Value == "right" then
animationId = "rbxassetid://13795555699"
elseif handedValue.Value == "left" then
animationId = "rbxassetid://13833716962"
else
animationId = "rbxassetid://13795555699"
end
Character.Animate.idle.Animation1.AnimationId = animationId
handedValue:GetPropertyChangedSignal("Value"):Connect(function()
if handedValue.Value == "right" then
animationId = "rbxassetid://13795555699"
elseif handedValue.Value == "left" then
animationId = "rbxassetid://13833716962"
else
animationId = "rbxassetid://13795555699"
end
Character.Animate.idle.Animation1.AnimationId = animationId
end)
end
local function handlePlayerAdded(Player)
Player.CharacterAdded:Connect(function(Character)
updateAnimation(Character)
end)
end
game.Players.PlayerAdded:Connect(handlePlayerAdded)
for _, Player in ipairs(game.Players:GetPlayers()) do
if Player.Character then
updateAnimation(Player.Character)
end
end
handedValue.Changed:Connect(function()
for _, Player in ipairs(game.Players:GetPlayers()) do
if Player.Character then
updateAnimation(Player.Character)
end
end
end)