GUI Giver script giving me too many gui's?

So I have this script that when you sit, it gives you A gui, but for some reason it gives me a way higher ammount than 1, random numbers. Any help?

local PlayerService = game:GetService("Players") -- get playerservice

script.Parent.Changed:Connect(function(newvalue) -- setup a function connected to the Changed event with a newvalue arg
    if not script.Parent.Occupant then return end -- make sure the change was an occupant
    local Player = PlayerService:GetPlayerFromCharacter(script.Parent.Occupant.Parent) -- get the player
    if not Player then return end -- check if player is nil (maybe an npc sat down idk)
    if Player.Name == script.OwnerName.Value then -- check if its the owner
    local dog = script.CarGui:Clone()
dog.Parent = Player.PlayerGui
    else -- if it isn't
        script.Parent.Occupant.Jump = true -- make them jump
    end -- close if statement
end) -- close function

does the output say anything? Try printing something in your script to determine why it is cloning multiple GUI’s.

I got put prints, and it repeats the ENTIRE script, so something in the parent must be changing more than once? I could try to put a wait() for like 3 seconds possibly?

Something is changing in your scripts it seems like, so yes I would put in a wait() at the top of your function. (maybe 3 seconds is good), see if it works.

So I tried this, but now its just completely stopped working.

local PlayerService = game:GetService("Players") -- get playerservice
rat = false

script.Parent.Changed:Connect(function(newvalue)
	if rat == false then
		rat = true
    local Player = PlayerService:GetPlayerFromCharacter(script.Parent.Occupant.Parent) -- get the player
    print("got player")
    if Player.Name == script.OwnerName.Value then -- check if its the owner
	print("checked player name")
    local dog = script.CarGui:Clone()
    print("cloned gui")
dog.Parent = Player.PlayerGui
    print("gave gui")
wait(2)
rat = false
    else -- if it isn't
script.Parent.Occupant.Jump = true -- make them jump
end
    end -- close if statement
end) -- close function

Your event will fire whenever the parent moves or any property of it is changed. You should be using this instead:

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(...);

This ensures that the anonymous function will only run whenever the Parent’s Occupant property changes.

1 Like

Am I doing something wrong?

local PlayerService = game:GetService("Players") -- get playerservice
rat = false

script.Parent.GetPropertyChangedSignal("Occupant"):Connect(...);
    local Player = PlayerService:GetPlayerFromCharacter(script.Parent.Occupant.Parent) -- get the player
    print("got player")
    if Player.Name == script.OwnerName.Value then -- check if its the owner
	print("checked player name")
    local dog = script.CarGui:Clone()
    print("cloned gui")
dog.Parent = Player.PlayerGui
    print("gave gui")
    else -- if it isn't
script.Parent.Occupant.Jump = true -- make them jump
end
  local PlayerService = game:GetService("Players") -- get playerservice
rat = false

script.Parent.GetPropertyChangedSignal("Occupant"):Connect(...);
    local Player = PlayerService:GetPlayerFromCharacter(script.Parent.Occupant.Parent) -- get the player
    print("got player")
    if Player.Name == script.OwnerName.Value and Player.PlayerGui:findFirstChild("CarGui") == nil then -- check if its the owner
	print("checked player name")
    local dog = script.CarGui:Clone()
    print("cloned gui")
dog.Parent = Player.PlayerGui
    print("gave gui")
    else -- if it isn't
script.Parent.Occupant.Jump = true -- make them jump
end

Did not work? No output errors either.

Didn’t think I needed to point this out, but the ellipsis was for you to fill in with your function.

1 Like

Im not really catching on, I apolgoize.

did it print at all? (extra words to post this).

Nothing in the output, so im assuming its the
`

script.Parent.GetPropertyChangedSignal(“Occupant”):Connect(…);

`

--game.Players for PlayerService?
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
    local player = game.Players:FindFirstChild(script.Parent.Occupant.Parent.Name);
    if player then
        if player.Name == script.OwnerName.Value and not player.PlayerGui:findFirstChild("CarGui") then
            local dog = script.CarGui:Clone();
            dog.Parent = Player.PlayerGui;
        else
            script.Parent.Occupant.Jump = true;
        end
    end
end

Sadly, did not work, and no output errors either.

Here is an article I found about the GetPropertyChangedSignal Instance:

it is mainly used for when an object is changed, like values. I mean the script’s parent is a value right? (sorry)

If im not mistaken, occupant is the player.

edited my reply. GetPropertyChangedSignal is a colon call, not dot.

a way you could do this is:

--game.Players for PlayerService?
  local function onParentChanged()
        local player = game.Players:FindFirstChild(script.Parent.Occupant.Parent.Name);
        if player then
            if player.Name == script.OwnerName.Value and not player.PlayerGui:FindFirstChild("CarGui") then
                local dog = script.CarGui:Clone()
                dog.Parent = Player.PlayerGui
            else
                script.Parent.Occupant.Jump = true
            end
        end
    end
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(onParentChanged)

Whoa, nice! That code looks a bit familiar, I wonder where it’s from… :stuck_out_tongue: