Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a despawn script know what player pressed button?

Asked by 4 years ago

Hello. Im trying to make a car despawner for my open world car game. I have a functional despawner but the problem i am having is that after the player presses the button it despawns all the cars that aren't occupied by any player. This includes random players outside their car for good reasons, like people role playing as police officers and conduction a traffic stop. Ive gotten a despawner working by using a GUI Text Button that has a LocalScript as a child. It then uses a RemoteEvent that triggers a server script applied for every different car i have added to my game. The problem is that the code has no way of telling which was being used by what player. This has been what has really confused me.

Here is the local script that is tied to the button

local deletecarButton= script.Parent:WaitForChild("DeleteCarButton")
local CrownVicDeleteScript = game.ReplicatedStorage.CrownVicPoliceDespawnerButton
local DodgeInterceptorDeleteScript = game.ReplicatedStorage.DodgeChargerInterceptorDespawn

deletecarButton.MouseButton1Click:Connect (function()

CrownVicDeleteScript:FireServer()
DodgeInterceptorDeleteScript:FireServer()

end)

As you can see as soon as my button is pressed it fires remote events for the 2 cars i have coded so far.

Here is the code on the car seats

local seat = script.Parent
local car = seat.Parent
local CrownVicDespawnerButton = game.ReplicatedStorage.CrownVicPoliceDespawnerButton

 CrownVicDespawnerButton.OnServerEvent:Connect(function()
    if not seat.Occupant then
        car:Destroy()
    end
end)

This code checks to make sure no one is in the car. Then despawns it. The problem im having is it checks for all cars of this model that have been spawned, not just the one the player using the button has spawned. So it deletes every non occupied car of this type including ones being still used just not currently occupied.

Ive tried everything i could think of but i can not find a way to make it so it knows what car is whose.

Any help is appreciated , Thanks

1 answer

Log in to vote
0
Answered by 4 years ago

Since you put the script inside each one of the cars, once you fire the event from the local script ALL cars will get the event to fire the OnServerEvent function.

To stop this from happening, you can add something inside the car's model when a player sits in it to convey that the car is currently occupied. For instance, taking advantage of the Seat.Touched event.

Example of script (would be placed on the seat of the car):

local Seat = script.Parent
local CarModel = script.Parent.Parent -- Or whatever the model is in relation to the seat
local BeingUsed = false

Seat.Touched:connect(function(obj)
    if obj.Parent:findFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(obj.Parent) then
        if not BeingUsed then
            local IntValue = Instance.new("IntValue", CarModel)
            IntValue.Name = "BeingUsed"
            BeingUsed = true
        end
    end
end)

After this script places an IntValue inside of the car, this marks the car as BeingUsed. In order to stop the car from being deleted after being tagged, just add this to the Script that controls the removal of the cars.

The script would look like this:

local seat = script.Parent
local car = seat.Parent
local CrownVicDespawnerButton = game.ReplicatedStorage.CrownVicPoliceDespawnerButton

CrownVicDespawnerButton.OnServerEvent:Connect(function()
    if not seat.Occupant and not car:FindFirstChild("BeingUsed") then
        car:Destroy()
    end
end)

This script makes it so that if there isn't something called "BeingUsed" found in the child, the car will be deleted unless occupied.

I hope this makes sense and will be of some use! :)

0
BeingUsed should be a bool because Int are numbers so it will error Luka_Gaming07 534 — 4y
0
BoolValue* Luka_Gaming07 534 — 4y
0
Thanks for the explanation it does make more since now. The one thing im confused about is how do i make it so if the player is done using the car (spawns a new one , leaves the game, or switches teams) it makes the car go back to being despawnable? Jpotts1515 0 — 4y
0
It will not error because you're not using the IntValue as a book but rather as a way to tag a car on whether is it being used or not. I will make a script and let you know on how you can untag the car in a bit! InfinityEngine 223 — 4y
0
Ok thanks Jpotts1515 0 — 4y
Ad

Answer this question