What do you want to achieve? I want to have a system where I can throw objects similar to flings things and people. They will go quicker depending on how much power you throw them with.
What is the issue? So when trying to throw the player, the player simply falls instead of flying in that particular direction I want it to go in. (I dont code the direction but lets say I throw it right, it would fly right depending on like the power of the throw)
What solutions have you tried so far? Yeah, I’ve looked online, in devforum but nothing seems to help.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
This is Client Script:
local player = game.Players.LocalPlayer;
local camera = game.Workspace.CurrentCamera;
local stopper = true;
local mouse = player:GetMouse();
local function loop(rig)
if not rig then return; end;
while not stopper do
local pos = player.Character.HumanoidRootPart.Position + camera.CFrame.LookVector * 10;
local cf = CFrame.new(pos);
rig.HumanoidRootPart:PivotTo(cf);
task.wait();
end;
stopper = false;
print('sent request to release')
game.ReplicatedStorage:WaitForChild('ReleaseEvent'):FireServer(rig);
end;
mouse.Button1Down:Connect(function()
if not stopper then
stopper = true;
else
local hit = mouse.Hit;
local cast = {
params = RaycastParams.new(),
origin = player.Character.Head.Position,
direction = camera.CFrame.LookVector * 100,
};
cast.params.FilterType = Enum.RaycastFilterType.Exclude;
cast.params.FilterDescendantsInstances = {player.Character};
local raycast = game.Workspace:Raycast(cast.origin, cast.direction, cast.params);
if raycast then
if raycast.Instance then
if raycast.Instance.Parent then
if raycast.Instance.Parent:FindFirstChild('Humanoid') then
if raycast.Instance.Parent.Name then
local rig = raycast.Instance.Parent;
print('sent request to grab')
game.ReplicatedStorage:WaitForChild('GrabEvent'):FireServer(rig);
stopper = false;
loop(rig);
end;
end;
end;
end;
end;
end;
end);
This is server script:
local grabEvent = game.ReplicatedStorage:WaitForChild('GrabEvent');
local releaseEvent = game.ReplicatedStorage:WaitForChild('ReleaseEvent');
local requestList = {};
local function checkCharacter(char)
if char:FindFirstChildWhichIsA('Humanoid') then
return true;
else
return false;
end;
end;
local function setNetworkOwnerOfModel(model, networkOwner)
for _, descendant in pairs(model:GetDescendants()) do
if descendant:IsA("BasePart") then
local success, errorReason = descendant:CanSetNetworkOwnership();
if success then
descendant:SetNetworkOwner(networkOwner);
else
error(errorReason);
end;
end;
end;
end; -- function provided by the devforum
local function remove(playerID)
task.spawn(function()
task.wait(0.3);
requestList[playerID] = nil;
end);
end;
grabEvent.OnServerEvent:Connect(function(player, char)
print('recieved grab')
if requestList[tostring(player.UserId)] then return; end;
if checkCharacter(char) then
local playerID = player.UserId;
table.insert(requestList, tostring(playerID));
setNetworkOwnerOfModel(char, player);
remove(tostring(playerID));
end;
end);
releaseEvent.OnServerEvent:Connect(function(player, char)
print('recieved release')
if requestList[tostring(player.UserId)] then return; end;
if checkCharacter(char) then
local playerID = player.UserId;
table.insert(requestList, tostring(playerID));
setNetworkOwnerOfModel(char, nil);
remove(tostring(playerID));
end;
end);
yeah but the problem is if I do an AND then it will just give an error because if one property doesn’t exist then it will throw an error so I have to do them in seperate lines.
It probably will lose momentum if you make the bodyvelocity to be a a game.Debris, if you can calculate enough velocity, gravity and friction will make it lose momentum but will be steady as long as you have enough velocity to added the player at a time.
I’ve used this mostly for dashing mechanics, it also flings people on contact of a surface of opposing force (simplified: if a player hits an object, it will make them fling)
Ok cool so I will use bodyvelocity. But how can I get the amount of power into the throw, e.g. if i flick my screen slow the throw doesn’t go very far, but if i got fast it has more momentum and goes quicker
I would use UserInputService.
Basically when the service finds a “key” (an event when a key or something is pressed) use that to find if the player clicked the mouse key that is representing as “release” and start a timer with task.spawn with a while true wait() statement or run service (either is fine) and use a global variable and another one as a stopper, and then use userinputservice.inputended event so when the player ends the input the global variable which stops the timer is set to false thus stopping the timer and use the other timing global variable and send a remote event to release with that value and then calculate or exponentially upgrade the value to your desired growth factor or common ratio, for example: BodyVelocityValue = seconds * growth factor
Thus BodyVelocityValue = 3 when seconds = 1.5 and growth factor is 2
I would use camera as when the player holds their mouse the camera angle is reported and when they release you just subtract the camera angle to find your desired value of “flicking”, and create a ratio with the number of seconds they held the player by, so if it is a fast flick but small camera angle change the player will move in linear motion, if it is a slow flick but wider camera angle, the player shall be flung in a wider range but in a lower velocity, recursively a fast flick and a wider camera angle results in a wider range of where the player is flung with high velocity.
The timer is to calculate if they did a fast flick or a slow flick, cause if they held the mouse key for a long time and they just moved their camera really slowly the game would not make the player get fling so far as that doesn’t makes sense.
Calculate time of mouse being pressed for how long
Calculate how much the player moved the camera
Calculate body velocity with the two ratioed values before for example:
Velocity = CameraFlickAmount/TimeOfMouseClick
Also calculate the distance of mouse.hit when the mouse was clicked and when the mouse was released and apply that to where the velocity’s angle will be going towards.
Feel free to reply, I’m in the UK and it’s 1am. I’m going to sleep thanks so much if I don’t see you again. I’ll check the chat in the morning.