Mopping Spill Script Issue

Hey everyone,

I’m currently trying to make a script for a cafe. This script is a ServerScript and goes under a part which is the spill. When the spill is touched and the character is holding the mop tool, it’ll freeze the character and play an animation on them.

The issue is that when the player touches the spill with the mop in hand, it does the animation/freezes a random person in the server who isn’t the one cleaning the spill. I’m not sure how to fix this; let me know if you have any ideas or further questions!

I’ve tried looking on the Developer Hub but haven’t found anything.

The script is below:

local TweenService = game:GetService("TweenService")
local spill = script.Parent
local plr = game.Players.LocalPlayer

local goal = {}
goal.Size = spill.Size - Vector3.new(0,5,5)

local tweenInfo = TweenInfo.new(5)

local tween = TweenService:Create(spill, tweenInfo, goal)

local activated = false

game.Players.PlayerAdded:Connect(function(player)
	spill.Touched:Connect(function(hit)
		if hit.Parent.Name == "Mop" then
			if activated == false then
				activated = true
				if spill.Transparency == 0 then
					game.Workspace:WaitForChild(player.Name).Humanoid.WalkSpeed = 0
					game.Workspace:WaitForChild(player.Name).Humanoid.JumpPower = 0
					tween:Play()
					animation = player.Character.Humanoid:LoadAnimation(script.Parent.Animation)
					animation:Play()
					wait(3)
					game.Workspace:WaitForChild(player.Name).Humanoid.WalkSpeed = 16
					game.Workspace:WaitForChild(player.Name).Humanoid.JumpPower = 50
					wait(2)
					spill.Transparency = 1
					spill.Size = Vector3.new(0.05, 3.039, 4.055)
					player.leaderstats.Points.Value += 1
				end
				activated = false
			end
		end
	end)
end)

Let me know if you can assist!

Since I’m assuming the mop is a tool, you should use game.Players:GetPlayerFromCharacter(hit.Parent.Parent), the reason it is random is cause you’re not actually checking if the Player defined is the Player who is touching the spill.

1 Like

Ah, thank you! So you mean:

game.Players:GetPlayerFromCharacter(hit.Parent.Parent).Humanoid:LoadAnimation(script.Parent.Animation)

?

Can you confirm the parents please.

hit.Parent - Mop Tool? Is there any parts within parts or grouped parts or are they all parented to the tool?
hit.Parent.Parent - Character

Wait!

animation = player.Character.Humanoid:LoadAnimation(script.Parent.Animation)

This seems to be working, thank you!

If you’re the only player in the server then it is going to work fine, having multiple players is the issue.

Hmm, let me test it with multiple players.

It looks like you are freezing the player from the PlayerAdded event.

I’m not 100% true but I think it should be;

game.Workspace:WaitForChild(hit.Parent.Name).Humanoid.WalkSpeed = 0
game.Workspace:WaitForChild(hit.Parent.Name).Humanoid.JumpPower = 0

Yes, this is more efficient and you won’t experience random players being frozen.

Rewrite of your code.

local TweenService = game:GetService("TweenService")
local spill = script.Parent

local goal = {}
goal.Size = spill.Size - Vector3.new(0,5,5)

local tweenInfo = TweenInfo.new(5)

local tween = TweenService:Create(spill, tweenInfo, goal)

local activated = false

	spill.Touched:Connect(function(hit)
		if hit.Parent.Name == "Mop" then
			if activated == false then
				activated = true
				if spill.Transparency == 0 then
                    local Player = game.Players:GetPlayerFromCharacter(hit.Parent.Parent)
                    if not Player then return activated = false end
					Player.Character.Humanoid.WalkSpeed = 0
					Player.Character.Humanoid.JumpPower = 0
					tween:Play()
					animation = Player.Character.Humanoid:LoadAnimation(script.Parent.Animation)
					animation:Play()
					wait(3)
					Player.Character.Humanoid.WalkSpeed = 16
					Player.Character.Humanoid.JumpPower = 50
					wait(2)
					spill.Transparency = 1
					spill.Size = Vector3.new(0.05, 3.039, 4.055)
					Player.leaderstats.Points.Value += 1
				end
				activated = false
			end
		end
	end)

Unfortunately, this didn’t work. :frowning:

Screenshot the script in the editor so I can see where the red lines are.

What do you mean by that? Sorry if I look dumb.

Take a screenshot of the script.

Example:(Ignore the Red Circle)

Oh! Here:

Shouldn’t you have a local before the first animation = line?

No, it works just fine without!

Try temporarily remove this line by added a - - (No spaces) at the start of the line.

If it still doesn’t work. Remove the local infront of Player and have it as Player =.

Try putting activated = false before the return statement

This worked, thank you so very much! You’ve been a great help.

1 Like