This script (not local) from a gui in StarterGui is trying to call (line 11)...
local ReplicatedStorage = game:GetService("ReplicatedStorage") local RE = ReplicatedStorage:WaitForChild("GiveName") function click(Player) local t = game.Teams[script.Parent.Text] script.Parent.Parent.Parent.Parent.Parent.Parent.TeamColor = t.TeamColor local spawns = game.Workspace.SpawnLocations:GetChildren() for i=1, #spawns do if spawns[i].TeamColor == t.TeamColor then script.Parent.Parent.Parent.Parent.Parent.Parent.Character:MoveTo(spawns[i].Position + Vector3.new(0,10,0)) RE:FireClient(Player) end end script.Parent.Parent.Parent:Remove() end script.Parent.MouseButton1Click:connect(click)
...this function from a script from ServerScriptService but is not working. What I am I doing wrong?
local ReplicatedStorage = game:GetService("ReplicatedStorage") local RE = ReplicatedStorage:WaitForChild("GiveName") function namer(plr) plr.CharacterAdded:Connect(function(char) local nameGui = Instance.new("BillboardGui") nameGui.StudsOffset = Vector3.new(0, 1.5, 0) nameGui.MaxDistance = 30 nameGui.Size = UDim2.new(0, 200, 0, 50) nameGui.Name = "NameGui" nameGui.Parent = char.Head local nameLabel = Instance.new("TextLabel") nameLabel.Text = plr.Name nameLabel.TextStrokeTransparency = 0 nameLabel.TextScaled = true nameLabel.Font = "Fantasy" nameLabel.Size = UDim2.new(0, 200, 0, 50) nameLabel.BackgroundTransparency = 1 nameLabel.TextColor3 = plr.TeamColor.Color nameLabel.Name = "NameLabel" nameLabel.Parent = nameGui end) end RE.OnServerEvent:Connect(namer)
Remove the player
inside the ()
on line 11 in the first script.
The main issue is that GuiButton:MouseButton1Click is a void
signal; it's return type is void
, meaning nothing. You're trying to derive a Player Object from a parameter that isn't supplied.
A ServerScript (regular Script), is designed to run on ROBLOX's end, meaning it doesn't pertain to any local machine. In other words, there is no "local Player Object" that can be retrieved as the Server is merely a complex architecture that is meant to manage connected Users—it isn't a User, it doesn't contain it's own Player.
StarterGui
replicates all content into a unique localized container to each Player called 'PlayerGui
', it is already a descendant of a user, so there wouldn't be a need to call :FireClient()
. That is also another topic that must be addressed. You'll receive an exception that exclaims that said method can only be called from the Server, you are essentially firing to yourself. You need to call :FireServer()
, to actively initiate an event on the Server, see [this]9https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events) Wiki for more details. Final note: There isn't a need to pass a Player through :FireServer()
, as the Server will receive the Client making a call to to it automatically, and is always supplied as the first parameter.
These are your resolved program, with efficiency modifications:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local RemoteEvent = ReplicatedStorage:WaitForChild("GiveName") local Players = game:GetService("Players") local Teams = game:GetService("Teams") local Button = script.Parent local Player = Players.LocalPlayer local Team = Teams[script.Parent.Text] local function OnClick() local Spawns = workspace.SpawnLocations:GetChildren() for _,Spawn in pairs(Spawns) do if (Spawn.TeamColor == Team.TeamColor) then local Character = Player.Character or Player.CharacterAdded:Wait() Character:MoveTo(Spawn.Position + Vector3.new(0,10,0)) Player.TeamColor = Team.TeamColor RemoteEvent:FireServer() end end --// Please don't use repetitive .Parent climbs. --// Store the Object in a variable with script:FindFirstAncestor("Name") --// Then call :Destroy() -- :Remove() was deprecated. end --// Lowercase :connect() was deprecated. Button.MouseButton1Click:Connect(OnClick)
Don't use ServerScripts for local userinput. As well as first argument of MouseButton1Click is not the player object or the player that clicked the local gui.
First of all, the first script should be in a local script, not a global one. From what I see, you are trying to attempt a client-to-server remote event. Whatever happens in the StarterGui is client-side, and only local scripts will run in them. For more info, go to this link:
https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events
Secondly, remove the Player
argument on line 4, and add this at the very beginning of the script.
local Players = game:GetService("Players")
local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()
It is much, much easier to get the player from this, especially that in a local script, which this script SHOULD be in. The problem was that you didn't have a way to access the player in the first place.
Thirdly, replace RE:FireClient(Player)
on line 11
with RE:FireServer(Player)
. FireServer() must match up with OnServerEvent(), and FireClient() must match up with OnClientEvent().