You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? working button for mobile players that can pick up props
-
What is the issue? i cant make working button for mobile
-
What solutions have you tried so far? tryed chat gpt it didynt work
-- This is an example Lua code block
```PhysicsService = game:GetService("PhysicsService")
InputService = game:GetService("UserInputService") -- Used for detecting keyboard presses, etc.
TweenService = game:GetService("TweenService") -- Used for detecting keyboard presses, etc.
RunService = game:GetService("RunService")
LocalPlayer = game.Players.LocalPlayer -- Get the local player
Camera = game.Workspace.CurrentCamera -- Get the player's camera
Mouse = LocalPlayer:GetMouse() -- Get the player's mouse
Props = workspace:WaitForChild("Props") -- Get the props folder in workspace
PickupPropEvent = game.ReplicatedStorage:WaitForChild("PickupProp") -- Get the PickupProp event in ReplicatedStorage
Config = workspace:WaitForChild("Config") -- Get the Config folder in the PropPickup model
Holding = nil
O_Friction = 0
O_Density = 0
HoldDistance = 0
LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson -- Make the player go into first person mode.
InputService.MouseIconEnabled = false -- Make the mouse invisible.
InputService.MouseBehavior = Enum.MouseBehavior.LockCenter -- Make the mouse locked to the center of the screen.
function KeyPressed(Input, GameProcessedEvent)
if GameProcessedEvent then -- This will make sure not to detect keys if the player is typing in a text box, such as the chat.
return
end
local Key = Input.KeyCode -- This gets the keycode of the input
if Key == Enum.KeyCode.E then -- This checks that the player presses E
local Target = Mouse.Target
local Character = LocalPlayer.Character
if not Character then
return
end
local Root = Character:FindFirstChild("HumanoidRootPart")
if not Root then
return
end
if Holding then -- If the player is holding a prop (this is for letting go of the prop with E)
Drop()
elseif Target and Target.Parent == Props then -- Check that the character exists, and then check if the player is looking at at a prop,
local Prop = Target
local Distance = (Root.Position - Prop.Position).Magnitude -- Get the distance from the player to the prop
if Distance < tonumber(Config.PickupDistance.Value) then
Pickup(Prop)
end
end
elseif Input.UserInputType == Enum.UserInputType.MouseButton1 then -- Mouse click
if Holding then -- Check if the player is holding a prop
Throw() -- If they are holding a prop, run the throw function.
end
end
end
InputService.InputBegan:connect(KeyPressed)
function Pickup(Prop)
if Holding then
return
end
if Prop:IsA("Model") then
return
end
Holding = Prop
Prop.Anchored = false
PhysicsService:SetPartCollisionGroup(Prop, "Prop_Holding") -- Sets the collision group of the prop, so it doesn't collide with the player.
PickupPropEvent:FireServer(Prop) -- Fire the event so the server knows we have picked up the prop.
local CPP = Holding.CustomPhysicalProperties
if not CPP then -- If the CustomPhysicalProperties on the part are disabled
Prop.CustomPhysicalProperties = PhysicalProperties.new(0.7, 0.3, 0.5) -- Sets the CustomPhysicalProperties to something
CPP = Holding.CustomPhysicalProperties -- Changes the variable to the new CustomPhysicalProperties
end
local DefaultHoldDistance = tonumber(Config.HoldDistance.Value)
local ForceHoldDistance = Prop:FindFirstChild("HoldDistance")
if ForceHoldDistance then
HoldDistance = tonumber(ForceHoldDistance.Value)
else
HoldDistance = DefaultHoldDistance
end
local N_Density = 0.1
O_Friction = CPP.Friction
O_Density = CPP.Density
Prop.CustomPhysicalProperties = PhysicalProperties.new(N_Density, 0.01, CPP.Elasticity)
-- The line above reduces the density and friction, so it's easier to hold and doesn't get stuck to walls.
local BodyGyro = Instance.new("BodyGyro")
BodyGyro.MaxTorque = Vector3.new(40000, 40000, 40000)
BodyGyro.D = 50
BodyGyro.P = 700
BodyGyro.Parent = Prop
local BodyPosition = Instance.new("BodyPosition")
BodyPosition.D = 60
BodyPosition.P = 0
BodyPosition.Parent = Prop
local Info = TweenInfo.new(0.7)
local Tween = TweenService:Create(BodyPosition, Info, {P = 1200}) -- This smoothly brings up the power of the BodyPosition so the prop doesn't get flung.
Tween:Play()
end
function LetGo()
local BodyGyro = Holding:FindFirstChild("BodyGyro")
local BodyPosition = Holding:FindFirstChild("BodyPosition")
PhysicsService:SetPartCollisionGroup(Holding, "Default")
Holding.CustomPhysicalProperties = PhysicalProperties.new(O_Density, O_Friction, Holding.CustomPhysicalProperties.Elasticity)
-- The line above sets the Density and Friction back to normal
BodyPosition:Destroy()
BodyGyro:Destroy()
end
function Drop()
if not Holding then
return
end
LetGo()
Holding.Velocity = Holding.Velocity / 3 -- Reduce the velocity when dropping so the prop can be flung very far
Holding = nil
end
function Throw()
if not Holding then
return
end
LetGo()
Holding.Velocity = Camera.CFrame.LookVector * 33 -- Add velocity to the prop where the player is looking
Holding = nil
end
function RenderStepped()
if Holding then -- Checks that the player is holding a prop
local BodyGyro = Holding:FindFirstChild("BodyGyro") -- Gets the BodyGyro
local BodyPosition = Holding:FindFirstChild("BodyPosition") -- Gets the BodyPosition
if BodyPosition and BodyGyro then -- Makes sure they both exist
local HoldPos = Camera.CFrame + Camera.CFrame.LookVector*HoldDistance -- Gets the cframe of where the prop should be
BodyGyro.CFrame = HoldPos -- Sets the bodygyro's cframe to the holding cframe
BodyPosition.Position = HoldPos.Position -- Sets the BodyPosition's position to the HoldPos position
end
end
end
RunService.RenderStepped:Connect(RenderStepped)