So, im trying to make some kind of drag system. And now i need to optimize it to the phones.
The code technically works, but on fact its not how it should work;
Im using the ContextActionService:BindAction(); It creates the buttons
but the drag button is not working. Can someone help me? (It should be like, when you press first time, its start dragging, and when the second, it stops dragging, but now its not working at all)
This is a local script, Client. If you have questions i can answer
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local character = script.Parent:FindFirstAncestorWhichIsA("Model")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
local player = Players:GetPlayerFromCharacter(character)
local mouse = player:GetMouse()
local network = script.Parent:WaitForChild("Network")
local MAX_FORCE = script.Parent.Force.Value
local MAX_RANGE = script.Parent.Range.Value
local grabForce = Instance.new("BodyPosition")
grabForce.MaxForce = Vector3.new(MAX_FORCE, MAX_FORCE, MAX_FORCE)
grabForce.P = grabForce.P
grabForce.D = 550
local conn
local object
local distance
local canTouch = false
local AnimationHold = player.Character.Humanoid:LoadAnimation(script.Animation)
function onGrabBegan()
if not object:FindFirstChild("DragDetector") then
AnimationHold:Play()
grabForce.Parent = object
object.CollisionGroup = "Items"
print(object.CollisionGroup)
if object:FindFirstChild("GUI") then
if object.GUI:FindFirstChildOfClass("BillboardGui") then
object.GUI:FindFirstChildOfClass("BillboardGui").TextLabel.Visible = true
end
end
canTouch = true
object.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and canTouch == true then
if game.Players:GetPlayerFromCharacter(hit.Parent) and game.Players:GetPlayerFromCharacter(hit.Parent)~=player then
if object:HasTag("Ragdoll") then
local scr = require(game.ReplicatedStorage.ragdoll)
scr.start(hit.Parent)
wait(4)
scr.stop(hit.Parent)
end
end
end
end)
local boxxs = Instance.new("Highlight")
boxxs.FillTransparency = 1
boxxs.DepthMode = Enum.HighlightDepthMode.Occluded
boxxs.Parent = object
boxxs.Name = "h"
conn = RunService.Heartbeat:Connect(function()
local cf = CFrame.new(humanoidRootPart.Position, mouse.Hit.Position)
grabForce.Position = cf.Position + cf.LookVector * distance
end)
network:FireServer(object)
end
end
script.Parent.RemoteFunction.OnClientInvoke = function()
return mouse.Hit.Position
end
function onGrabEnded()
canTouch= false
if conn then
conn:Disconnect()
end
if object:FindFirstChild("GUI") then
if object.GUI:FindFirstChildOfClass("BillboardGui") then
object.GUI:FindFirstChildOfClass("BillboardGui").TextLabel.Visible = false
end
end
AnimationHold:Stop()
object.CollisionGroup = "Default"
grabForce.Parent = nil
if object:FindFirstChildOfClass("Highlight") then
if object:FindFirstChildOfClass("Highlight").Name == "h" then
object:FindFirstChildOfClass("Highlight"):Destroy()
end
end
network:FireServer()
end
function grabHandler(action, inputState, inputObject)
if action == "grab" then
if inputState == Enum.UserInputState.Begin then
if conn then
conn:Disconnect()
end
object = mouse.Target
if object then
if object:FindFirstChild("T") then
distance = (humanoidRootPart.Position - object.Position).Magnitude
if distance < MAX_RANGE then
onGrabBegan()
end
end
end
end
if inputState == Enum.UserInputState.End then
onGrabEnded()
end
end
end
function Weld()
script.Parent.Weld:FireServer(object)
end
function Use()
script.Parent.Eat:FireServer(object)
end
function Drop()
script.Parent.Drop:FireServer(object)
end
game:GetService("UserInputService").InputBegan:Connect(function(key,tu)
if tu then
return
end
if key.KeyCode == Enum.KeyCode.Z then
Weld()
end
if key.KeyCode == Enum.KeyCode.F then
Use()
end
if key.KeyCode == Enum.KeyCode.Q then
Drop()
end
end)
ContextActionService:BindAction(
"grab",
grabHandler,
true,
Enum.UserInputType.MouseButton1,
Enum.KeyCode.ButtonR2
)
ContextActionService:BindActionAtPriority("Wasya", Weld, true, 1, Enum.KeyCode.Z)
ContextActionService:BindActionAtPriority("Wasya", Use, true, 1, Enum.KeyCode.F)
ContextActionService:BindActionAtPriority("Wasya", Drop, true, 1, Enum.KeyCode.Q)
player.PlayerGui.Phone.W.MouseButton1Click:Connect(function()
script.Parent.Weld:FireServer(object)
end)
player.PlayerGui.Phone.Interact.MouseButton1Click:Connect(function()
script.Parent.Eat:FireServer(object)
end)
player.PlayerGui.Phone.Disenteract.MouseButton1Click:Connect(function()
script.Parent.Drop:FireServer(object)
end)
local s = false
humanoid.Died:Connect(function()
ContextActionService:UnbindAction("grab")
if conn then
conn:Disconnect()
end
grabForce:Destroy()
end)