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

Unable to cast value to Object with animations?

SERVER SCRIPT:

game.ReplicatedStorage.AnimationPlay.OnServerEvent:Connect(function(player, Animation)
	print(player)
	local Track = game.Players[player.Name].Character.Humanoid:LoadAnimation(Animation)
	Track:Play()
end)

LOCAL SCRIPT:

wait(2)
player = game.Players.LocalPlayer
Animation = script.Animation


workspace.Part.Touched:Connect(function(Hit)
	game.ReplicatedStorage.AnimationPlay:FireServer(game.Players.LocalPlayer.Name, Animation)
end)

Everytime I touch the brick, it errors with "Unable to cast value to Object". I do not know why.

1 answer

✓ Accepted answer

This is a very common problem for users that a new to using remote events. OnServerEvent will have the player object passed as the first argument by Roblox. It knows who sent the request as it will only ever be from one player.

Example

-- server script
somreRemoteEvent.OnServerEvent:Connect(function(plr, arg1)
	print(plr.Name, "sent", arg1)
end)

-- local script
somreRemoteEvent:FireServer("Hi")

As you can see your argument Animation will be holding the first argument sent in your case the name of the player which is why you get the error. RobloxLua is trying to use string where an Animation instance is expected.

However there are more things that need to be considered with your code. There are a small amount if things that will bypass FilteringEndabled check the Exceptions listed here. Player animations should all be played in a local script as each player controls their own physics.

The last problems lie in your local script. One of the problems with using the Touched event is that it will fire for all pase parts that touch it. The result will be that the code is ran multiple times when it is not needed ie the player is playing the animation. This event will also run for other player as well so you also need to include the correct validation to check that the player is the local player. This can be done by using GetPlayerFromCharacter.

When using an instance on the client side you often will need to use WaitForChild as it will take a small amount of time for Roblox to replicate the instance to the player. Using wait is not the solution here. Lastly you should be using local variables. Please look this up on how they work and how they should be used.

local part = workspace:WaitForChild("Part")
local plrServ = game:GetService("Players") -- use GetService as the name can be changed by the client causing things to break
local animation = script:WaitForChild("Animation")

part.Touched:Connect(function(hit)
	-- return out of this function if the player is not the local player
	if plrServ.LocalPlayer ~= plrServ:GetPlayerFromCharacter(hit.Parent) then return end

	-- your code to playe the animation and handle any other checks
end)

The code above is just a quick example of what your setup should look like. There still a lot of points that I have mentioned above but I do not know how this code will be used so this is as far as I will go.

If you have any other questions with the code or points made then please leave a comment. I hope this helps.

1 comment

0

Thank you. It helped a ton!

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