Hello.
I have a tool with a local script that check if the tool is equipped and then checks if you’re pointing a land to plant a crop.
The problem is that it says “This mouse is no longer active” when i click on a land
This is the code:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
local PlantRE = game.ReplicatedStorage:WaitForChild("Events").Plant
script.Parent.Equipped:Connect(function(Mouse)
script.Parent.Activated:Connect(function()
if not Mouse then return end
local Target = Mouse.Target -- Here is where the error appears
-- "This mouse is no longer active"
if Target then
if Target.Parent.Name == "Land" then
local Land = Target.Parent
local DistanceBetweenPlayerAndLand = (HRP.Position - Land.PrimaryPart.Position).magnitude
if DistanceBetweenPlayerAndLand < 10 then
if Land.IsTilled == true and Land.IsSeeded == false then
print("Planted")
PlantRE:FireServer(Land,"Wheat",script.Parent)
end
end
end
end
end)
end)
I always use this method so i don’t know what to do.
Thanks in advice.
Don’t use the mouse parameter.
Instead, get the mouse beforehand:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()
local HRP = Character:WaitForChild("HumanoidRootPart")
local PlantRE = game.ReplicatedStorage:WaitForChild("Events").Plant
script.Parent.Equipped:Connect(function()
script.Parent.Activated:Connect(function()
if not Mouse then return end
local Target = Mouse.Target -- Here is where the error appears
-- "This mouse is no longer active"
if Target then
if Target.Parent.Name == "Land" then
local Land = Target.Parent
local DistanceBetweenPlayerAndLand = (HRP.Position - Land.PrimaryPart.Position).magnitude
if DistanceBetweenPlayerAndLand < 10 then
if Land.IsTilled == true and Land.IsSeeded == false then
print("Planted")
PlantRE:FireServer(Land,"Wheat",script.Parent)
end
end
end
end
end)
end)
1 Like
Now it doesn’t gives the error but the remote event doesn’t get fired
I have another local script for a rake which is almost the same and works perfectly
-- // Variables \\ --
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
-------------------------
-- // Functions \\ --
-- Detect land --
script.Parent.Equipped:Connect(function(Mouse)
script.Parent.Activated:Connect(function()
if not Mouse then return end
local Target = Mouse.Target
if Target then
if Target.Parent.Name == "Land" and Target.Parent:IsA("Model") then
local Land = Target.Parent
local DistanceBetweenPlayerAndLand = (HRP.Position - Land.PrimaryPart.Position).magnitude
if DistanceBetweenPlayerAndLand < 10 then
if Land.IsTilled.Value == false then
script.Till:FireServer(Land)
end
end
end
end
end)
end)