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