I am making it so that if you are the spy you get a weapon, but it won’t give you the weapon with the error " [21:55:57.520 - Players.SadWowow21.PlayerGui.ClassGui.Frame.TextLabel.LocalScript:17: attempt to index nil with 'Backpack".
Code:
local remoteEvent = game.ReplicatedStorage:WaitForChild("GUIEvent")
local Player = game.Players.LocalPlayer
remoteEvent.OnClientEvent:Connect(function(selectedRole)
script.Parent.Visible = true
if selectedRole == "Scientist" then
script.Parent.TextSize = 50
script.Parent.Text = "Scientist, stick with soldiers!"
wait(4)
script.Parent.Parent.Visible = false
elseif selectedRole == "Soldier" then
script.Parent.TextSize = 50
script.Parent.Text = "Soldier, protect scientists against the spy!"
wait(4)
script.Parent.Parent.Visible = false
elseif selectedRole == "Spy" then
local tool = game.ReplicatedStorage.Sword:Clone()
tool.Parent = Player:FindFirstChild(Player.Name).Backpack
script.Parent.TextSize = 50
script.Parent.Text = "Spy, kill soldiers and scientists, and gather data!"
wait(4)
script.Parent.Visible = false
end
end)
a localscript is the client, only a single player will be given the effect of this, this is things such as guis, and more. scripts are ran on the server. I assume you’re new to scripting so I recommend using documentary sites to help you with such things.
Ok, but would you know why the tools are not working? They will equip and not do anything after. i tried it with multiple tools and I know it has something to do with the local script
You would run this in a server script. Look at it this way, things on the server replicate to the client. So when the client gives itself the item and tries to use it, the item is overwritten by the absence of the item on the server. local scripts are really only for Gui and Camera things. Your remote event is fine for Gui text and other things, but not for cloning items. Make another script in ServerScriptService for giving the item to the player.
nono, use the same script within a local script, but with a server script in server script service add a remote event to replicatedstorage and from the client fire the remote event. on the server script put a onserverevent connection. then give the player the tool, remember the first argument in a remote is the player.
local remoteEvent = game.ReplicatedStorage:WaitForChild("GUIEvent")
local WeaponEvent = game.ReplicatedStorage:WaitForChild("WeaponEvent")
local Player = game.Players.LocalPlayer
remoteEvent.OnClientEvent:Connect(function(selectedRole)
script.Parent.Visible = true
if selectedRole == "Scientist" then
script.Parent.TextSize = 50
script.Parent.Text = "Scientist, stick with soldiers!"
wait(4)
script.Parent.Parent.Visible = false
elseif selectedRole == "Soldier" then
WeaponEvent:FireClient(Player,selectedRole)
script.Parent.TextSize = 50
script.Parent.Text = "Soldier, protect scientists against the spy!"
wait(4)
script.Parent.Parent.Visible = false
elseif selectedRole == "Spy" then
WeaponEvent:FireClient(Player,selectedRole)
script.Parent.TextSize = 50
script.Parent.Text = "Spy, kill soldiers and scientists, and gather data!"
wait(4)
script.Parent.Visible = false
end
end)
and for the script
local WeaponEvent = game.ReplicatedStorage:WaitForChild("WeaponEvent")
local Player = game.Players.LocalPlayer
WeaponEvent.OnClientEvent:Connect(function(selectedRole)
if selectedRole == "Soldier" then
local tool = game.Lighting.Pistol:Clone()
tool.Parent = Player.Backpack
script.Parent.Parent.Visible = false
elseif selectedRole == "Spy" then
local tool2 = game.Lighting.ClassicSword:Clone()
tool2.Parent = Player.Backpack
end
end)
local WeaponEvent = game.ReplicatedStorage:WaitForChild("WeaponEvent")
WeaponEvent.OnServerEvent:Connect(function(Player, selectedRole)
if selectedRole == "Soldier" then
local tool = game.Lighting.Pistol:Clone()
tool.Parent = Player.Backpack
script.Parent.Parent.Visible = false
elseif selectedRole == "Spy" then
local tool2 = game.Lighting.ClassicSword:Clone()
tool2.Parent = Player.Backpack
end
end)
You are connecting a server event not a client event here.
Please use this for help understanding: