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

Why doesn't my Character added function work?

Asked by 4 years ago

I'm creating a shop where a pre-made button will appear for a player when they buy an item. My intention is to make the button invisible at first, and when the player buys an item, it becomes visible. I got this to work, however I want the button to stay visible when the player dies. I used a CharacterAdded function to go directly into the player's StarterGui and make the button visible. This works outside of the CharacterAdded function, but not inside of it, as I mention in the comments below. I also tried making a remote event that triggers a localscript inside of the button to make the button visible, but still no luck (I don't get any errors, compiler just skips over these lines of code.) Does anyone know why this happens, and what I can do differently to make the code inside the function work?

if Player.Inventory:FindFirstChild("Bool") then Player.PlayerGui.ScreenGui.Button.Visible = true --this works print("-Button loaded-") --this prints

Player.CharacterAdded:Connect(function()
    Player.PlayerGui.ScreenGui.Button.Visible = true --this does NOT work
    game.ReplicatedStorage.GETButton:FireClient(Player) --this does NOT work
    print("-Player Respawned. Button returned-") --this prints
end)

end

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You have to define the player. Here is what you need to add

game.Players.PlayerAdded:connect(function(Player) -- Add this
Player.CharacterAdded:Connect(function()
    Player.PlayerGui.ScreenGui.Button.Visible = true --this does NOT work
    game.ReplicatedStorage.GETButton:FireClient(Player) --this does NOT work
    print("-Player Respawned. Button returned-") --this prints
end)

Also, a server script can't access PlayerGui. Use a local script inside of the StarterGui and use this code.

local Player = game.Players.LocalPlayer -- Add this
Player.CharacterAdded:Connect(function()
    Player.PlayerGui.ScreenGui.Button.Visible = true 
    game.ReplicatedStorage.GETButton:FireClient(Player)
    print("-Player Respawned. Button returned-") 
end)
Ad
Log in to vote
0
Answered by 4 years ago

Hey guys thanks for the input but I did some research and found a solution myself. I learned that using a module script works better in this particular case. First I put a Module script in ServerScriptService that goes like this:

local module = {} function module:ReloadBTN(player) if player.PassInventory and player.PassInventory:FindFirstChild("ButtonBool") then player:WaitForChild("PlayerGui").ScreenGui.theButton.Visible = true end end return module

Then I placed a regular script into StarterCharacterScripts that goes like this:

local player = game.Players:GetPlayerFromCharacter(script.Parent) BTNMod = require(game.ServerScriptService.BTNMod)

if player:WaitForChild("PassInventory"):FindFirstChild("ButtonBool") then BTNMod:ReloadBTB(player) print("-ReLoad activated-") end

Now, after a player buys a particular item, the button will appear for them every time they respawn. Remember to use the require function in the script you want to trigger the module script in.

Answer this question