local handle = script.Parent.Handle local debounce = false script.Parent.Activated:Connect(function() if not debounce then debounce = true local cloned = game.ServerStorage.bomb:Clone() cloned.Parent = workspace cloned.CFrame = handle.CFrame*CFrame.new(0, 1, 0) cloned.Velocity = Vector3.new(0, math.random(50, 100), 0) wait(5) debounce = false end end)
The above code is supposed to create a clone of a sphere named "bomb" in ServerStorage, set the Parent of the clone to game.Workspace, match its CFrame to the tool's Handle CFrame (plus 1 point up on the y axis). It also gives the clone a velocity of 0 for the x axis, a random number between 50 and 100 for the y axis, and 0 for the z axis. The thing is, it obviously shoots up, and that's it. I want this block to shoot out in the direction that the player holding the tool is looking in. How could I do this?
One problem that I have seen is that a client is trying to access the server storage. Instead, use replicated storage. Don't forget that the names of objects are case sensitive. Secondly, using a bodyvelocity allows a constant force to be applied to a specific part. I have fixed these problems in the code below.
local handle = script.Parent.Handle local debounce = false local replicatedStorage = game:GetService("ReplicatedStorage") local bomb = replicatedStorage:WaitForChild("Bomb") local maxSpeed = 25 -- Not studs per second local waitTime = 5 script.Parent.Activated:Connect(function() if not debounce then debounce = true local cloned = bomb:Clone() cloned.Parent = workspace cloned.CFrame = handle.CFrame*CFrame.new(0, 1, 0) local bVelocity = Instance.new("BodyVelocity") local tarVel = Vector3.new(handle.CFrame.LookVector.X, handle.CFrame.LookVector.Y + workspace.Gravity, handle.CFrame.LookVector.Z) bVelocity.Velocity = tarVel * maxSpeed bVelocity.P = 100 bVelocity.Parent = cloned wait(waitTime) debounce = false end end)