|
| 31 May 2014 10:57 AM |
Currently, a script I use uses "hit" to detect when someone sits in a seat. I wanted to know if there was a way to tell if they sat in the seat.
The reason is because my script shows a GUI to any player that isn't VIP, telling them to buy it. It only removes when they get out of the seat, HOWEVER if a person walks past it and touches it without sitting down they will get the GUI and it doesn't disappear.
Heres what I have so far: (not all of the script is here, just the part thats broken)
Seat.Touched:connect(function(hit) playerHasTouched = 0 if hit and hit.Parent and lookForPlayer(hit.Parent.Name) then playerHasTouched = 1 -- I tried to detect if the touch happens twice (i.e they are sitting down) but it didn't work wait(2) if hit and hit.Parent and lookForPlayer(hit.Parent.Name) then playerHasTouched = 2 local player = lookForPlayer(hit.Parent.Name) if (not IsVIP(player)) and Kill_If_Not_VIP and (playerHasTouched == 2) then script.Parent.Parent.Antiwobble.Anchored = true script.BuyVIP:clone().Parent = game.Players:findFirstChild(hit.Parent.Name).PlayerGui wait(10) script.Parent.Disabled = true else script.Parent.Parent.Antiwobble.Anchored = false script.Parent.Disabled = false end playerHasTouched = 0 end end end) |
|
|
| Report Abuse |
|
|
|
| 31 May 2014 11:01 AM |
if Humanoid.Sit == true then
Then make sure it's the right seat. You can do this by checking the seats.Hit.Parent and the character name of whos sitting. |
|
|
| Report Abuse |
|
|
|
| 31 May 2014 11:09 AM |
Thanks for replying
I changed the first bit to this:
Door.Touched:connect(function(hit) if Humanoid.Sit==true and seats.hit.Parent and lookForPlayer(hit.Parent.Name) then
but this returns an error with "humanoid" Is this because I haven't declared who the humanoid is? How would I do this? |
|
|
| Report Abuse |
|
|
|
| 31 May 2014 11:11 AM |
function getHumanoidFromPart(part) if part and part.Parent:FindFirstChild("Humanoid") then return part.Parent.Humanoid end end
local humanoid = getHumanoidFromPart(hit) -- inside the connection
I made it a function so your code doesn't get confusing for you and I believe it's better to give people functions they can use instead of writing it in their code for them. |
|
|
| Report Abuse |
|
|
|
| 31 May 2014 11:13 AM |
Loop through the characters to check when the are sitting. Check if the right character is sitting on right seat. Then run rest of script. |
|
|
| Report Abuse |
|
|
|
| 31 May 2014 11:52 AM |
I added in the function and then the line of code into the main script so it looks like this:
Seat.Touched:connect(function(hit) local humanoid = getHumanoidFromPart(script.Parent) if humanoid.sit==true and seats.hit.Parent and lookForPlayer(hit.Parent.Name) then local player = lookForPlayer(hit.Parent.Name) if (not IsVIP(player)) and Kill_If_Not_VIP and not script.Parent.Locked then script.Parent.Parent.Antiwobble.Anchored = true script.BuyVIP:clone().Parent = game.Players:findFirstChild(hit.Parent.Name).PlayerGui wait(10) script.Parent.Disabled = true else script.Parent.Parent.Antiwobble.Anchored = false script.Parent.Disabled = false end end end)
I get the error that humanoid is a nil value though. I'm not sure if I've incorrectly modified the function or not as the part is script.Parent? MD
|
|
|
| Report Abuse |
|
|
Gorake
|
  |
| Joined: 10 Feb 2014 |
| Total Posts: 219 |
|
|
| 31 May 2014 11:55 AM |
Don't use the Touched event at all - since every time a player sits in a seat a Weld is created, child of the seat, between the seat and the torso, you can exploit the ChildAdded function like so:
seat.ChildAdded:connect(function(child) if child.Name == "SeatWeld" then local torso = child.Part1 local character = torso.Parent local player = game.Players:GetPlayerFromCharacter(character) --Do whatever end end) |
|
|
| Report Abuse |
|
|
|
| 31 May 2014 11:57 AM |
Doesn't anyone know how seats work?
Seat.ChildAdded:connect(function(w) if w:IsA("Weld") then char = w.Part1.Parent print(char.Name.." is on this seat") end end) |
|
|
| Report Abuse |
|
|
Gorake
|
  |
| Joined: 10 Feb 2014 |
| Total Posts: 219 |
|
|
| 31 May 2014 11:57 AM |
Oh, and for when they're getting out of the seat, use DescendantRemoving
seat.DescendantRemoving:connect(function(child) if child.Name == "SeatWeld" then local torso = child.Part1 local character = torso.Parent local player = game.Players:GetPlayerFromCharacter(character) --Do whatever end end) |
|
|
| Report Abuse |
|
|
| |
|
|
| 31 May 2014 12:03 PM |
@gorake
It works! Thank you! |
|
|
| Report Abuse |
|
|