I'm trying to make a button that refreshes the player who clicked it, but it isn't working and I don't know why. There are no errors in the output or anything underlined red in the script. Here is the script:
plr = game.Players.LocalPlayer script.Parent.MouseButton1Click:connect(function() pcall(function() local Pos = plr.Character:GetPrimaryPartCFrame() plr:LoadCharacter() plr.Character:SetPrimaryPartCFrame(Pos) if plr.Character:FindFirstChild("ForceField") then plr.Character["ForceField"]:Destroy() end end) end)
Any help would be appreciated!
You're calling it from a local script. That won't work. You will need a remote event.
-- # remember to define `remoteEvent` remoteEvent.OnServerEvent:Connect(function(client) client:LoadCharacter() end)
Here i'm just listening on the server side for OnServerEvent
to be fired. When it is, the character of the player who fired the remote gets reloaded.
And from the client you would call remoteEvent:FireServer()
to fire the server event.
local function onClick() -- # localise! remoteEvent:FireServer() -- # remember to define `remoteEvent` end script.Parent.Activated:Connect(onClick) -- # Activated is the recommended way of getting GUI click input.
Let's see if this works... I've seen many people saying that you need to use events, but for a beginning scripter, I wouldn't expect you to understand events. Heck, I don't even understand events yet! So I just do a little local value
changing here and a little checking for changes there, and that's all that's needed!
So, let's try a something a bit like this:
To set things up, you need a StringValue
in ReplicatedStorage
named "PlrName"
In the Client-Side
(local, in the Button
):
plr = game.Players.LocalPlayer script.Parent.MouseButton1Click:Connect(function() game:GetService("ReplicatedStorage"):FindFirstChild("PlrName").Value = plr.Name end) -- very short and simple.
In the Server-Side
(plain, in ServerScriptService):
local plrNameV = game:GetService("ReplicatedStorage"):FindFirstChild("PlrName") plrNameV:GetPropertyChangedSignal("Value"):Connect(function() -- checks for the value to change local plr = game.Players:FindFirstChild(plrNameV.Value) -- Gets the player from the value set in the LocalScript local Pos = plr.Character:GetPrimaryPartCFrame() -- you put - Set a value for the player's position plr:LoadCharacter() -- you put - load the player's character plr.Character:SetPrimaryPartCFrame(Pos) -- you put- set the player to their original position if plr.Character:FindFirstChild("ForceField") then plr.Character:FindFirstChild("ForceField):Destroy() -- Your Remove Forcefield module end end)