How to get the string part of string.gsub

So currently I am making something that takes a chat message from a player, finds the animation off of it and then finds the id using a similar method, but when finding the id I have to use gsub to cut off the text so I am left with only the id of the animation. then when I try and use it in a string it says


because gsub returns both a string and the amount of times the string pattern was removed. This makes the error with the string. Here are the script segments.

--variables and stuff
local function GetID(Message, Animation, Table)
	for i, v in ipairs(Table) do
		if string.find(string.lower(v), string.lower(Animation)) ~= nil then
			return string.gsub(string.lower(v), string.lower(Animation), "")
		end
	end
end
--more code
for i, v in ipairs(animations) do
	local animation = string.find(string.lower(Message),string.lower(v))
	print(animation)
	if animation ~= nil then
		local animid = GetID(Message, v, animationids)
		local animationobject = Instance.new("Animation")
		animation.AnimationId = "rbxassetid://" .. tostring(animid) -- error line
		game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(animationobject)
		animation:Play()
	end
end

How would you get the string part of the value?

Before checking details in your script, are you sure you don’t want to use this:

animationobject.AnimationId = "rbxassetid://"..tostring(animid)

instead of this:

animation.AnimationId = "rbxassetid://"..tostring(animid)

Cause, you created an animation with that variable "animationobject ":
local animationobject = Instance.new("Animation")

1 Like

Yup, thank you. I have done this before where I create a topic asking a question but it was just me forgetting to change certain parts. Makes sense I made this mistake because I renamed the variable after writing the code.
Have a good day.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.