I have special items in my game and I want it to where when the player touches that item, the item destroys and a GUI shows up saying they got it
I coded it correctly and it works, but it shows for everybody, The gui showing, and the part destroying. Is there any way i can make it to where when a player touches that part the part destroys and shows the GUI for them, instead of every player?
The script is in a local script item btw, not a regular script, and the script location is in the GUI
You could change the Script into a Server Script, and put this code inside the Part
You should have a basic understanding of these things as well:
Touched Events
GetPlayerFromCharacter() function
Connecting Functions
local Part = script.Parent --Defining the Part
local function RemoveGUI(Hit) --Creating the local function, which "Hit" is the first parameter for the "Touched" event
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent) --Getting the Player from the Character Model
if Player then --Checking if we have a player
local PlayerGui = Player.PlayerGui
PlayerGui.YourGuiHere:Destroy() --Configuring the Player's GUI for them only
end
end
Part.Touched:Connect(RemoveGUI)
Thanks, and by changing it into a server script do you mean moving the script into "ServerScriptService? And should the GUI script stay a Local Script when I move it into There?
I am also confused by this script. I see RemoveGUI, when the only thing im trying to remove when the player touches it in this is the Part/Item. I want the GUI to Show when the player touches the part. Sorry If I am wrong, I am fairly new to Lua and still trying to learn.
local Part = script.Parent --Defining the Part
local function RemoveTool(Hit) --Creating the local function, which "Hit" is the first parameter for the "Touched" event
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent) --Getting the Player from the Character Model
if Player then --Checking if we have a player
if Player.Character:FindFirstChild("Tool") then
Player.Character.Tool:Destroy()
else
Player.Backpack.Tool:Destroy()
end
Player.PlayerGui.RandomGui.Enabled = true
end
end
Part.Touched:Connect(RemoveTool)
My bad, I went ahead and added in a couple more things to my script
Upon activation, the said “Item” will be removed & the GUI will be enabled for that player hopefully
I do also recommend looking at the Dev API for helpful information
I modified the script and for the most part it works, the GUI shows for the player who touched it only, but the part still deletes for everybody
heres the modified script BTW:
local Part = script.Parent --Defining the Part
local function RemoveTool(Hit) --Creating the local function, which "Hit" is the first parameter for the "Touched" event
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent) --Getting the Player from the Character Model
if Player then --Checking if we have a player
wait(.5)
Part:Destroy()
end
Player.PlayerGui.PinkeggGet.Frame.Visible = true
end
Part.Touched:Connect(RemoveTool)
Alright, if you want the Part to be gone for 1 Player but not for everyone else we can do some client manipulation here
Now, this time what we’ll do is insert a LocalScript inside StarterPlayerScripts using somewhat of the same code as I mentioned earlier:
local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local Part = workspace.Part --Defining the Part
local function RemoveTool(Hit) --Creating the local function, which "Hit" is the first parameter for the "Touched" event
local Check = game.Players:GetPlayerFromCharacter(Hit.Parent) --Getting the Player from the Character Model
if Check then --Checking if we have a player
PlayerGui.PinkeggGet.Frame.Visible = true
wait(.5)
Part:Destroy()
end
end
Part.Touched:Connect(RemoveTool)
Now this time, we can actually get the Local Player with this type of script so we don’t have to worry about getting it from the Touched Event but I believe we should at least still check if a player has touched the part or not
We also defined the PlayerGui outside our function since we can actually access it from there
Now when the Part gets touched, the Part should be destroyed on the client-side (Aka destroyed from you, but visible to everyone else) and the GUI should be enabled a swell
I believe so, and just to make sure that everything works as well we can add some print() statements in our code:
local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local Part = workspace.Part --Defining the Part
print("Code online")
local function RemoveTool(Hit) --Creating the local function, which "Hit" is the first parameter for the "Touched" event
print("Part touched")
local Check = game.Players:GetPlayerFromCharacter(Hit.Parent) --Getting the Player from the Character Model
if Check then --Checking if we have a player
print("Enabling the GUI for: "..Player.Name)
PlayerGui.PinkeggGet.Frame.Visible = true
wait(.5)
Part:Destroy()
end
end
Part.Touched:Connect(RemoveTool)
The Script Doesn’t work for me. Now Whenever I touch the part, the Gui shows and the part destroys for everybody again, and the script is in starterplayerscripts
Can you check your Output & Server Screen to see if the part’s still there? Also do make sure to disable that other Touched script if you haven’t done so already, which might be why it’s happening
There doesn’t seem to be anything really wrong, unless if you forgot to define where to Parent the Part variable to??? Check your Output if you can and see what gets printed out