I’m having a bit of trouble with my script. My knowledge is extremely limited, so I’m doing the best that I can with what I know but I can’t, for the life of me, seem to resolve the issue with this script. I’ve tried inserting print statements all over to pinpoint it but those efforts also failed me.
Basically, this script is supposed to clone a gui from ServerStorage and display it to the player if they don’t already have it. I can post the code from the LocalScript in the gui if necessary, but all it really does is play a sound, fade away, then delete itself via Destroy().
The script runs fine the first time. The gui is cloned and the player is teleported (essentially to prevent the script from running over and over again), but after the gui destroys itself, FindFirstChild() seems to think it still thinks it exists, causing the block associated with it not to run the second time.
Here’s the code:
Players = game:GetService("Players")
ServerStorage = game:GetService("ServerStorage")
Part = script.Parent
scriptDebounce = Part.scriptDebounce
Part.Touched:Connect(function(touchedBy)
local Character = touchedBy.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
local Player = Players:GetPlayerFromCharacter(Character)
if scriptDebounce.Value == true then
if Player ~= nil then
scriptDebounce.Value = false
if Player.PlayerGui:FindFirstChild("SoundGui") == nil then
local SoundGui = ServerStorage.Guis.SoundGui:Clone()
SoundGui.Parent = Player.PlayerGui
end
if Humanoid ~= nil then
Humanoid.Torso.CFrame = CFrame.new(Part.Position.X + 6, Part.Position.Y, Part.Position.Z)
end
scriptDebounce.Value = true
end
end
end)
Any assistance would be greatly appreciated. I’ve done a little searching on Google and the Developer Forum in an attempt to find a solution but I think we both can conclude I wasn’t able to figure it out. Many thanks!
Every change you made in a LocalScript just take effect on the Client side, this means that the SoundGui is still there for the server. You should delete it through the server script.
a simple way to prevent this (if not already done) is to instantiate the gui locally and remove it locally. you don’t really need a ScreenGUI to be server sided.
you basically need to rewrite your code that it fires an event from server to client regarding instantiating the GUI and from there on sort the rest out this also gets rid of the :FindFirstChild problem.
You shouldn’t need to involve the server at all for these kinds of things. The server still sees the SoundGui as existent because the client deletes it. Just handle the part touch and verification all from the client-side and move the SoundGui to ReplicatedStorage instead of ServerStorage.
Thanks for your response. This actually makes a lot of sense and I can’t believe I didn’t think of that considering client-server replication has been brought to my attention before. I suppose I’ll start checking what the server sees more often when testing my game.
After realizing that server-client replication was the issue I’m facing, I began rewriting everything. I’m lucky the scripts in their entirety aren’t large at all so this isn’t that big of a burden. Thanks.
Very eye-opening post. As previously mentioned, I’ve barely started learning to script, so I don’t really have an idea of when things should be performed on the server vs. the client and where exactly objects are best placed. An irrelevant detail, but I haven’t been that invested in development since before all this stuff existed. I’ve bookmarked your thread and will keep it in mind from now on. Thank you for the tip!