Hello! I had made custom tools recently and they are working great. However whenever I tried to tween them to make them go towards the mouse’s target they just fall? I’m trying to make it so you can throw the tools at specific objects around a map. Any help or other ways of doing this is greatly appreciated!
Here is how my objects folder is setup:
Here is how the tools are setup:
Here is my server code:
local repStorage = game:GetService("ReplicatedStorage")
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Circular, Enum.EasingDirection.In, 0, false, 0)
local itemIndex = require(repStorage.Modules.ItemModules.ItemIndex)
local objectIndex = require(repStorage.Modules.ItemModules.ObjectIndex)
local throwItem = repStorage.Remotes.ThrowItem
for i, world in pairs(workspace.Worlds:GetChildren()) do
if world.Objects then
for i, object in pairs(world.Objects:GetChildren()) do
if object:IsA("Model") then
local health = Instance.new("NumberValue", object)
health.Name = "Health"
health.Value = objectIndex[world.Name][object.Name].Health
end
end
end
end
throwItem.OnServerEvent:Connect(function(player, object, item)
local itemModel = player.Character:FindFirstChild("Item"):Clone()
itemModel.Parent = workspace
itemModel.Handle:FindFirstChild("PlayerWeld"):Destroy()
wait()
local tween = tweenService:Create(itemModel.Handle, tweenInfo, {Position = object.Position})
tween:Play()
tween.Completed:Wait()
object.Parent.Health.Value -= item.Strength
end)
Here is my client code:
local repStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
local selectedObject = player:WaitForChild("Values"):WaitForChild("SelectedObject")
local equippedItem = player:WaitForChild("Values"):WaitForChild("EquippedItem")
local healthGui = repStorage:FindFirstChild("HealthGui")
local objectIndex = require(repStorage.Modules.ItemModules.ObjectIndex)
local itemIndex = require(repStorage.Modules.ItemModules.ItemIndex)
local throwItem = repStorage.Remotes.ThrowItem
local farthestDistance = 35
local throwStatus = false
local function findItem()
for i, item in pairs(itemIndex[selectedObject.Value.Parent.Parent.Parent.Name]) do
if item.Name == equippedItem.Value then
return item
end
end
end
local function updateHealthGui(toggle)
if toggle and selectedObject.Value ~= nil then
healthGui.Parent = selectedObject.Value.Parent
healthGui.Title.Text = selectedObject.Value.Parent.Name
healthGui.Background.Health.Text = selectedObject.Value.Parent.Health.Value
healthGui.Background.Bar.Size = UDim2.new(selectedObject.Value.Parent.Health.Value / objectIndex[selectedObject.Value.Parent.Parent.Parent.Name][selectedObject.Value.Parent.Name].Health, 0, 1, 0)
if selectedObject.Value.Parent:FindFirstChildOfClass("Humanoid") then
healthGui.ExtentsOffset = Vector3.new(0, 1.5, 0)
else
healthGui.ExtentsOffset = Vector3.new(0, 0, 0)
end
else
healthGui.Parent = repStorage
healthGui.Background.Bar.Size = UDim2.new(1, 0, 1, 0)
end
end
local function throw()
if selectedObject.Value ~= nil then
throwItem:FireServer(selectedObject.Value, findItem())
updateHealthGui(true)
return true
else
return false
end
end
mouse.Button1Down:Connect(function()
local target = mouse.Target
local status = false
if target then
if target.Parent.Parent.Name == "Objects" then
if (player.Character:WaitForChild("HumanoidRootPart").Position - target.Position).Magnitude <= farthestDistance then
if equippedItem.Value ~= "" then
status = true
end
end
end
end
if status then
selectedObject.Value = target
else
selectedObject.Value = nil
end
end)
runService.RenderStepped:Connect(function()
if selectedObject.Value ~= nil then
if (player.Character:WaitForChild("HumanoidRootPart").Position - selectedObject.Value.Position).Magnitude > farthestDistance then
selectedObject.Value = nil
end
end
end)
selectedObject.Changed:Connect(function()
if selectedObject.Value ~= nil then
updateHealthGui(true)
if throwStatus == false then
repeat task.wait(1); throwStatus = throw() until throwStatus ~= true
end
else
updateHealthGui(false)
end
end)