· 6 years ago · Jul 20, 2019, 07:46 PM
1-- Hello Hidden Developers application readers i hope the comments are enough to show what is happening lol
2-- this is basically a thing i made to place down traps in certain areas, bear traps and such
3
4--variables
5local Trap = game:GetService("ReplicatedStorage"):WaitForChild("Trap",10)
6local TrapSpot = workspace:WaitForChild("TrapSpot")
7
8local Player = game.Players.LocalPlayer
9local Character = script.Parent
10local HRP = Character:FindFirstChild("HumanoidRootPart")
11
12local trapDown = false
13local inRange = false
14local renderstepped = game:GetService("RunService").RenderStepped
15local UserInputService = game:GetService("UserInputService").InputBegan
16
17local animationID = script:WaitForChild("Animation")
18
19
20
21function createGui() -- creating the gui
22 local screenGui = Instance.new("ScreenGui")
23 local back = Instance.new("Frame")
24 local front = Instance.new("Frame")
25
26 --positions
27 back.Position = UDim2.new(0.424, 0,0.679, 0)
28 front.Position = UDim2.new(0,0,0,0)
29
30 --size
31 back.Size = UDim2.new(0.151, 0,0.021, 0)
32 front.Size = UDim2.new(0.03,0,1,0)
33
34 --backgroundcolor from red,green,blue
35 back.BackgroundColor3 = Color3.fromRGB(4,4,4)
36 front.BackgroundColor3 = Color3.fromRGB(255,255,255)
37
38 --set the names of the instances
39 back.Name = "Back"
40 front.Name = "Front"
41 screenGui.Name = "Progress"
42
43 -- parent them
44 screenGui.Parent = Player:FindFirstChild("PlayerGui")
45 back.Parent = screenGui
46 front.Parent = back
47end
48
49-- destroy the gui, when finished
50function DestroyGui()
51 if Player:FindFirstChild("PlayerGui"):FindFirstChild("Progress") then -- if the progress gui exists then pass true
52 Player.PlayerGui.Progress:Destroy()
53 end
54end
55
56-- main function to run
57function plantTrap()
58 local animationTrack = Character.Humanoid:LoadAnimation(animationID) -- load the animation into the humanoid
59 animationTrack.Looped = true -- make sure its looped
60 wait(0.1) -- wait a second
61 animationTrack:Play() -- play it ?
62 Character.Humanoid.WalkSpeed = 0 -- stop them from moving
63 print("Planting") -- debug
64 createGui()
65 local gui = Player:FindFirstChild("PlayerGui"):FindFirstChild("Progress") -- define the gui
66 if gui then -- if the gui exists
67 for i = 0, 1, 0.1 do -- for loop to run 10 times with an increment of 0.1
68 gui.Back.Front.Size = UDim2.new(i,0,1,0) -- set the xscale of the gui, (progress bar)
69 print(i) -- debug
70 wait(0.3) -- wait
71 if not inRange then -- if in any way during the process you are no longer in range then stop
72 DestroyGui()
73 break -- break the loop
74 end
75 end
76 if not inRange then DestroyGui() return end -- if you're not in range after the process stop
77 print("Planted") -- debug
78 local trap = Trap:Clone() -- create the new instance from repstorage
79 trap.Parent = workspace -- parent to the workspace
80 trap.Position = Vector3.new(TrapSpot.Position.X,TrapSpot.Position.Y,TrapSpot.Position.Z) -- postiion it to the trapspot position at all axis
81 TrapSpot:Destroy() -- get rid of the spot
82 trapDown = true -- make sure the script knows there is a trapthere
83 DestroyGui() -- remove gui
84 local AnimationTracks = Character.Humanoid:GetPlayingAnimationTracks() -- get all playing animations in the humanoid (returns table)
85 for i, track in pairs (AnimationTracks) do -- are u still reading these comments? this loops through the table returned above
86 track:Stop() -- stops them all
87 end
88 Character.Humanoid.WalkSpeed = 16 -- let the dude walk
89 end
90end
91
92renderstepped:Connect(function() -- runservice event to run everytime a frame is loaded in the client
93 local distance = (HRP.Position - TrapSpot.Position).Magnitude -- distance between humanoid root part and the trap spot
94
95 if distance <= 7 then -- if the distance is equal to or less than 7 studs then pass true
96 if not trapDown and not inRange then -- extra precautions
97 inRange = true -- in range is true
98 print("in range") -- debug
99 end
100 else
101 if inRange then -- if inrange is true and ur not in the distance then
102 inRange = false -- let the script know ur not
103 print("out of range") -- debug lol
104 end
105 end
106end)
107
108UserInputService:Connect(function(keyDown,gameProcessed) -- input began event,
109 if keyDown.KeyCode == Enum.KeyCode.F and not gameProcessed and not trapDown and inRange then -- precautions to make sure you meet requirements
110 plantTrap() -- call the function
111 end
112end)
113
114
115-- thanks for reading it lol