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.
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