Script: trying to make it tween to a mouse position on click. PLEASE
local tool = script.Parent
local anim = tool:WaitForChild("Throwanim")
local player1 = game.Players.LocalPlayer
local char = player1.Character or player1.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local loadanim = humanoid:LoadAnimation(anim)
local mouse = player1:GetMouse()
tool.Activated:Connect(function(player)
if not loadanim.IsPlaying then
loadanim:Play()
local Player = game.Players.LocalPlayer
local tool = script.Parent
local Throw = tool.Handle and tool["Meshes/tomato"]:Clone()
local TweenService = game:GetService("TweenService")
local Part = script.Parent.Handle
local Info = TweenInfo.new(
2,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0,
false
)
Throw.Parent = workspace
local newpos = mouse.Hit.Position
local tween = TweenService:Create(Throw, Info, {Position = newpos})
tween:Play() -- Parenthesis was not supposed to be here
end
end)
local tool = script.Parent
local anim = tool:WaitForChild("Throwanim")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local loadanim = humanoid:LoadAnimation(anim)
local mouse = player:GetMouse()
tool.Activated:Connect(function()
if not loadanim.IsPlaying then
loadanim:Play()
local Throw = tool.Handle and tool:WaitForChild("Meshes"):WaitForChild("tomato"):Clone()
local TweenService = game:GetService("TweenService")
local Part = script.Parent.Handle
local Info = TweenInfo.new(
2,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0,
false
)
Throw.Parent = workspace
local newpos = mouse.Hit.Position
local tween = TweenService:Create(Throw, Info, {Position = newpos})
tween:Play()
end
end)
Here are the changes made:
Removed the unnecessary local variable player1 and used game.Players.LocalPlayer directly instead.
Removed the duplicated tool local variable declaration.
Changed tool["Meshes/tomato"]:Clone() to tool:WaitForChild("Meshes"):WaitForChild("tomato"):Clone() to get the correct child object.
Removed the redundant Player local variable declaration.
The player’s Mouse only exists for that client. It can’t be used server-side, so you probably couldn’t server-side this script without drastic changes.
It’s doing this because you are running this on a local script, meaning that it is going to be localized. Scrap my mistake earlier, since you do in fact need mouse position just fire a remote to send the mouse pos to the server.
ex:
Tool.Activated:Connect(function()
Tool.ToolRemote:FireServer(Mouse.Hit.Position) -- obviously have toolremote be an actual remotevent inside the tool
end)
local tool = script.Parent
local activator = tool:WaitForChild("Activate")
local mouse = game.Players.LocalPlayer:GetMouse()
local remoteEvent = game.ReplicatedStorage:WaitForChild("ThrowToolEvent")
activator.Activated:Connect(function()
remoteEvent:FireServer(mouse.Hit.Position)
end)
Server-side script:
local remoteEvent = Instance.new("RemoteEvent")
remoteEvent.Name = "ThrowToolEvent"
remoteEvent.Parent = game.ReplicatedStorage
remoteEvent.OnServerEvent:Connect(function(player, mousePosition)
local tool = player.Character:FindFirstChildOfClass("Tool")
if tool then
local humanoid = player.Character:WaitForChild("Humanoid")
local anim = tool:WaitForChild("Throwanim")
local loadanim = humanoid:LoadAnimation(anim)
if not loadanim.IsPlaying then
loadanim:Play()
local Throw = tool.Handle and tool:WaitForChild("Meshes"):WaitForChild("tomato"):Clone()
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(
2,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0,
false
)
Throw.Parent = workspace
local tween = TweenService:Create(Throw, Info, {Position = mousePosition})
tween:Play()
end
end
end)