VERY annoying bug in studio (Maybe only me)

Not sure where to put post this.

But for some reason my scripts work one minute then they don’t the next. Ill open studio and they dont work, ill reopen studio and suddenly they work.

One session they work and the next they dont,. no erros at all in the output. Any idea why this happens?

1 Like

I’ve noticed this happens in scripts with PlayerRemoving events and for saving datastores: two systems that work better in the actual Roblox player than in Studio. This is normal for some scripts.

Would this be an issue in a real game situation? Like if a player joins the game the scripts have a 50/50 chance of happening?

Can you send the script this is occurring with? If it uses datastores or PlayerRemoving, most likely it is just a Studio bug.

I have multiple scripts but this is the one that handles the resource hit

local module = {}

local replicatedStorage = game:GetService("ReplicatedStorage")
local tweenService = game:GetService("TweenService")
local debris = game:GetService("Debris")
local workspace = game:GetService("Workspace")

local hitResourceEvent = Instance.new("RemoteEvent", replicatedStorage)
hitResourceEvent.Name = "HitResource"

local tweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 2, true)

function module.handleResourceHit()
	hitResourceEvent.OnServerEvent:Connect(function(player, target)
		print("Hit event received")
		if target and target.Parent == workspace:WaitForChild("Resources") then
			print("Valid target parent confirmed")
			local isResource = target:FindFirstChild("IsResource")
			local health = target:FindFirstChild("Health")

			if isResource and health then
				print("Resource and Health attributes confirmed")
				health.Value = health.Value - 20  -- Decrease health by 20 (adjust as needed)
				print("Health reduced, current health: ", health.Value)

				-- Ensure no multiple tweens are running
				if target:FindFirstChild("ShakeTween") then
					target:FindFirstChild("ShakeTween"):Destroy()
				end

				-- Shake the part
				local originalPosition = target.Position
				local shakeTween = tweenService:Create(target, tweenInfo, {Position = originalPosition + Vector3.new(0, 0.2, 0)})
				local tweenMarker = Instance.new("BoolValue")
				tweenMarker.Name = "ShakeTween"
				tweenMarker.Parent = target

				shakeTween:Play()
				shakeTween.Completed:Connect(function()
					target.Position = originalPosition  -- Reset position after shaking
					print("Shake tween completed")
					tweenMarker:Destroy()  -- Remove the marker to allow future tweens
				end)

				-- Activate the ParticleEmitter
				local particleEmitter = target:FindFirstChildOfClass("ParticleEmitter")
				if particleEmitter then
					print("ParticleEmitter found, emitting particles")
					particleEmitter:Emit(50)  -- Emit particles, adjust the amount as needed
				else
					warn("No ParticleEmitter found for resource: " .. target.Name)
				end

				-- Play hit sound
				local hitSound = target:FindFirstChildOfClass("Sound")
				if hitSound then
					print("Hit sound found, playing sound")
					hitSound:Play()
				else
					warn("No hit sound found for resource: " .. target.Name)
				end

				-- Check if health is depleted
				if health.Value <= 0 then
					print("Health depleted, destroying resource")
					shakeTween:Cancel()
					target:Destroy()
				end
			else
				warn("Invalid resource or missing Health/IsResource attributes")
			end
		else
			warn("Attempt to modify an invalid target")
		end
	end)
end

return module

put task.wait() at the beginning of the script

1 Like