Hey guys, today I was working on a script for animated textures in my game, but have ran into an issue. My current problem is that duplicates of the same texture (in this case water) play their animations at varying speeds instead of playing at the same time. I have little idea why this behavior is occurring, as I’d assume the script should be firing off for every tagged part at the same time. I really want to fix this, since its obviously visually distracting. Below will be the current module script which is required by a server script, for additional some context. Any and all help will be greatly appreciated <3
local animatedTexturesModule = {}
--Services
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
--Variables
local animatedTextures = CollectionService:GetTagged("AnimatedTexture")
local frames = 1 --Number of frames in a spritesheet
local currentFrame = 1
local rows = 1 --Number of rows in a spritesheet
local columns = 1 --Number of columns in a spritesheet
local currentRow = 0
local currentColumn = 0
local fps = 30
local targetRate = 0.15 --The lower this number, the faster the animation will play
local accumulatedTime = 0
--RunService connection
local animatedTexturesRunService = RunService.Heartbeat:Connect(function(deltaTime)
for _, texture in ipairs(animatedTextures) do
if texture:IsA("Texture") then
local size = texture.Parent.Size --The spritesheet should be on the FRONT of the part
accumulatedTime = accumulatedTime + deltaTime
if accumulatedTime >= targetRate then
if texture:HasTag("Water") then --Water
frames = 32
rows = 6
columns = 6
texture.StudsPerTileU = columns * size.X
texture.StudsPerTileV = rows * size.Y
currentColumn = currentColumn + 1
if currentColumn > columns then
currentColumn = 1
currentRow = currentRow + 1
end
if currentFrame > frames then
currentRow = 1
currentColumn = 1
currentFrame = 1
end
texture.OffsetStudsU = size.X * (currentColumn - 1)
texture.OffsetStudsV = size.Y * (currentRow - 1)
currentFrame = currentFrame + 1
end
if texture:HasTag("Seafoam") then --Seafoam
frames = 27
rows = 9
columns = 3
texture.StudsPerTileU = columns * size.X
texture.StudsPerTileV = rows * size.Y
currentColumn = currentColumn + 1
if currentColumn > columns then
currentColumn = 1
currentRow = currentRow + 1
end
if currentFrame > frames then
currentRow = 1
currentColumn = 1
currentFrame = 1
end
texture.OffsetStudsU = size.X * (currentColumn - 1)
texture.OffsetStudsV = size.Y * (currentRow - 1)
currentFrame = currentFrame + 1
end
accumulatedTime = accumulatedTime - targetRate
end
end
end
end)
return animatedTexturesModule
Your code is looping through the textures synchronously, or in other words it has to change the frame of a texture one at a time for each tagged part.
I think the easiest way to fix this (I don’t know about the speed but worry about that later) is to instead connect the updating for each part, keeping a table of connections to avoid memory leaks:
local function connect(texture: Texture)
connections[texture] = RunService.Heartbeat:Connect(function(deltaTime)
-- do your code here...
end)
end
local connections: {[Texture]: RBXScriptConnection} = {}
collectionService:GetInstanceAddedSignal("AnimatedTexture"):Connect(function(texture)
connect(texture)
end)
collectionService:GetInstanceRemovedSignal("AnimatedTexture"):Connect(function(texture)
connections[texture]:Disconnect()
connections[texture] = nil
end)
for _, texture in collectionService:GetTagged("AnimatedTexture") do
connect(texture)
end
How exactly do you want me to write it differently than AI?
Using online resources like the DevForum itself lets AI get this code. You have no other reason to suspect this is AI besides “it looks similar”.
I usually write with --!strict. I put type annotations everywhere in my code. If I really didn’t care enough to answer with my own code, I’d copy and paste this with comments, without type annotations.
Just because someone’s work appears like the AI’s work doesn’t mean it is AI.
because your variable names are exact the same, you wrote the service name the exact same way instead of rewriting the original code casing.
surprize me, why “connections”?, why not something like “TextureThreads”
Just make a function which gets called every frame; perhaps use RunService. In that function loop trough every Part, for that I would recommed Tags. Then change the Part’s Texture offset. Signals can get off sync, but this can’t and thus resolving your initial issue.
dawg are we SERIOUS?? we can’t abbreviate anymore?
im not interacting with you anymore bc this wastes our times (not sure if you agree since you seem to have lots of time) and bc this is extremely off-topic, false accusations with no basis besides “oh yeah it seems similar!!!”
local CollectionService = game:GetService("CollectionService")
local Textures: {string} = {"rbxassetid://1", "rbxassetid://2"}
local index = 0
while task.wait(0.5) do
index += 1
for _, part: BasePart in CollectionService:GetTagged("Water") do
if not part:FindFirstChildWhichIsA("Texture") then
Instance.new("Texture", part)
end
part:FindFirstChildWhichIsA("Texture").TextureId = (index % #Textures) + 1
end
end
i do agree with your reply, but please refrain from jumping to conclusions because my code is similar to AI’s work which is based upon online resources
you can compare a larger excerpt of my code to AI in dms if you really care that much but you do you