So here I have a script, if player is within the distance of 5, its makes GUI visible. This script works fine but I want to be able for this script to check if the player that is within the Distance of 5 has the tool name "Gun" currently equipped.
If the player has the gun equipped and within the distance of 5, then breakjoints? how would I go about doing this?
I have tried v.Character.Gun in replacement of v.Character.Torso.. but that somehow didn't work.
brick = script.Parent distance = 5 local newThread = coroutine.create(function() while wait() do for i, v in pairs(game.Players:GetPlayers()) do if v.Character and v.Character.Torso and (v.Character.Torso.Position - brick.Position).magnitude < distance then v.PlayerGui.CtS.Frame.Visible = true end if v.Character and v.Character.Torso and (v.Character.Torso.Position - brick.Position).magnitude > distance then v.PlayerGui.CtS.Frame.Visible = false end end end end) coroutine.resume(newThread)
Per BlueTaslem, you may as well use spawn()
rather than coroutines if you aren't going to be taking advantage of coroutine.yield.
Also, there is a function called FindFirstChild we can use to see if an object exists (Gun) inside the Character (meaning it is equipped).
FindFirstChild returns nil
if it finds nothing, so I will be using ~= nil
to check if it found something.
Also, on lines 7 and 10, you only need to use one if statement, because otherwise you are checking the same thing twice. (checking the inverse, which is true in this case at any time the other is false)
brick = script.Parent distance = 5 spawn(function() while wait() do for i, v in pairs(game.Players:GetPlayers()) do if v.Character and v.Character:FindFirstChild("Gun") ~= nil and (v.Character.Torso.Position - brick.Position).magnitude < distance then v.PlayerGui.CtS.Frame.Visible = true else v.PlayerGui.CtS.Frame.Visible = false end end end end)
EDIT BreakJoints is a function of Model, so to use it on the character we just target the model, which is player.Character.
brick = script.Parent distance = 5 spawn(function() while wait() do for i, v in pairs(game.Players:GetPlayers()) do if v.Character and v.Character:FindFirstChild("Gun") ~= nil and (v.Character.Torso.Position - brick.Position).magnitude < distance then v.PlayerGui.CtS.Frame.Visible = true v.Character:BreakJoints() --This will kill the player else v.PlayerGui.CtS.Frame.Visible = false end end end end)