Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to get the player's character from a VehicleSeat using a Touched() event?

local debounce = false

local leave = script.Parent.CloseHitBox
local stop = script.Parent.OpenHitBox
local returner = script.Parent.ReturnHitBox
 function close()
    print("Close")
end

function open()
    print("Open")
end
stop.Touched:Connect(function(hit)
    if hit:IsA("VehicleSeat") and debounce == false then
        debounce = true
        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        if player ~= nil then
            player.leaderstats.Money = player.leaderstats.Money - 10 
        end
        wait(1)
        open()
        returner.Touched:Connect(function(hit)
            if hit:IsA("VehicleSeat") and debounce == true then
                debounce = false
                close()
            end
        end)
        leave.TouchEnded:Connect(function(hit)
            if hit:IsA("VehicleSeat") and debounce == true then
                debounce = false
                close()
            end
        end)
    end    
end)

I am trying to get the leaderstats.Money integer value from the player's character, but it only returns an "unable to define an index value"

Do I need to get the seatweld of the player for the seat and how do I do this if so?

1 comment

1

VehicleSeat.Occupant is all you should need, since Occupant is the Humanoid sitting in the seat

3 answers

✓ Accepted answer

VehicleSeat.Occupant

The Occupant of a VehicleSeat is the Humanoid currently sitting in the seat. You cannot do anything with Occupant except read from it.

To find the character, you would use GetPlayerFromCharacter() on the parent of the Humanoid:

if hit.Occupant then -- Checks to see if there is an occupant of the seat
	if game.Players:GetPlayerFromCharacter(hit.Occupant.Parent) then -- Checks to see if the occupant's Parent is associated with a player, therefore a character

If this condition passes, then change the value:

local player = game.Players:GetPlayerFromCharacter(hit.Occupant.Parent)
player.leaderstats.Money.Value = player.leaderstats.Money.Value - 10

This is a simple mistake that is relatively easy to fix. Don't stress yourself, we all learn from our mistakes.

use a loop to loop all the players and then paste all your code inside it

if its a local script then do

locala plr =  game.Players.LocalPlayer

i use looping on server sided so all players are looped through

inside the touched event is where you put the loop tho

The previous answer is poorly worded, and a bad answer in general [Ferbi's answer]. As a comment stated, you would need to use Seat.Occupant, which returns the Humanoid Instance (if there is one). The character would simply be Humanoid.Parent. Example:

local h = script.Parent.Occupant;
local char = h.Parent;
print(char.Name, "is sitting on the seat.");

You can learn more about Occupant here.

.Touched is possible, but is a bad alternative.

This discussion is preserved as it was. New answers, comments, and votes are closed.