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

Help guys I was making a car owner thingy but it keeps on killing me because of accessories?

Asked by 6 years ago
local seat = script.Parent
local name_value = script.Parent.Parent.Name_Valu
seat.Touched:connect(function(hit)
    name_value.Value = hit.Parent.Name
    print(name_value.Value)
end)
local debounce = false
local seat = script.Parent
local name_value = seat.Parent.Name_Valu
seat.Touched:connect(function(hit)
    if not debounce then
        debounce = true
        if hit.Parent.Name == name_value.Value then
            local hum = hit.Parent:FindFirstChild("Humanoid")
            if hum ~= nil then
                hum.Health = hum.Health
            end
        else
            local hum = hit.Parent:FindFirstChild("Humanoid")
            if hum ~= nil then
                hum.Health = 0
            end
        end
        debounce = false
        print(name_value.Value)
    end
end)
0
try using hit.Parent:FindFirstChild("Humanoid") to test if it's a player what touched Leamir 3138 — 6y
0
i am not doing that DarkModule 34 — 6y

1 answer

Log in to vote
0
Answered by
T0XN 276 Moderation Voter
6 years ago
Edited 5 years ago

You have a few problems wrong with your script here, I've outlined them with comments for easier, briefer communication. If you've caught any problems with my script then please do let me know.. nobody is a perfect coder!

It isn't very clear what you mean when you say the problem is caused "because of accessories", but I'll assume its because you aren't doing proper Humanoid checks to make sure "name_value" only becomes the name of the player and not whatever touches it.

Hope this helps.

ORIGINAL:

local seat = script.Parent -- * Why did you define "seat" twice?
local name_value = script.Parent.Parent.Name_Valu -- **
seat.Touched:connect(function(hit) -- *** :connect() is deprecated, use :Connect() instead, also please don't define two of the same functions for the same object
    name_value.Value = hit.Parent.Name -- You need another check here to see if it was a player that hit the seat and not just some regular part
    print(name_value.Value)
end)
local debounce = false
local seat = script.Parent
local name_value = seat.Parent.Name_Valu -- ** "name_value" defined twice
seat.Touched:connect(function(hit) -- ***
    if not debounce then
        debounce = true
        if hit.Parent.Name == name_value.Value then
            local hum = hit.Parent:FindFirstChild("Humanoid") -- *****
            if hum ~= nil then
                hum.Health = hum.Health -- **** This is completely redundant
            end
        else
            local hum = hit.Parent:FindFirstChild("Humanoid") -- ***** "hum" defined twice, also the hum check should have been be handled earlier on to avoid problems
            if hum ~= nil then
                hum.Health = 0
            end
        end
        debounce = false -- ****** Debounce becomes redundant if you don't have a wait(time). Regardless, this script doesn't need debounce because it only runs once once (when the player touches the seat)
        print(name_value.Value)
    end
end)

EDITED:

local name_value = script.Parent.Parent.Name_Valu.Value
script.Parent.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum then
        if name_value == nil then
            name_value = hit.Parent.Name
        elseif name_value and hit.Parent.Name ~= name_value then
            hum.health = 0
        end
    end
end)
Ad

Answer this question