How do I call a function which is in replicated storage - lua

So I'm trying to give the players different weapons based on what team they are on. I'm trying to do this via creating a instance string value and adding it to a folder which is the player's name in ReplicatedStorage, But it only gets updated on the Player's side, Not for the server. I'm trying to fix this via making a script in ReplicatedStorage and calling the function named handleTeams but I always get the error which says that handleTeams is not the child of the script.
LocalScript (LocalScript) (Players > Player1 > PlayerGui > InsertedObjects > team > Russia)
local p = script.Parent.Parent.Parent.Parent.Parent.Name
script.Parent.MouseButton1Click:Connect(function()
game.Players[p].TeamColor = BrickColor.new("Really blue")
game.Workspace[p].Humanoid.Health = 0
script.Parent.Parent.Parent.Enabled = false
local Player = Instance.new("Folder")
local GameTag = Instance.new("StringValue")
GameTag.Value = "USA"
GameTag.Name = "TeamName"
Player.Name = game.Players:FindFirstChild(p).Name
script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.ReplicatedStorage.Script(Player, GameTag)
end)
TeamGear (Script) (Workspace > TeamGear)
function onSpawned(plr)
if script.Parent.Parent.ReplicatedStorage.Teams[plr.Name].TeamName == "Russia" then
local tools = script.Parent.Parent.Teams.Russia:GetChildren()
for _,c in pairs(tools) do
c:Clone().Parent = plr.Backpack
end
end
if script.Parent.Parent.ReplicatedStorage.Teams[plr.Name].TeamName == "USA" then
local tools = script.Parent.Parent.Teams.USA:GetChildren()
for _,c in pairs(tools) do
c:Clone().Parent = plr.Backpack
end
end
end
Script (Script) (ReplicatedStorage > Script)
function handleTeams(player, tag)
player.Parent = script.Parent.Teams
tag.Parent = player
end

If you want to define a script in ReplicatedStorage that can be called from other scripts, it has to be a ModuleScript.
For example if you want to have a script in ReplicatedStorage that prints "Hello World", then you'd create a ModuleScript (not a Script) like this:
ReplicatedStorage > ModuleScript
local module = {}
function module.Hello()
print("Hello, World!")
end
return module
Then from a LocalScript or Script you can call it like so:
local HelloModule = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))
HelloModule.Hello()

Related

StarterGui:SetCore must be called from a local script

So... I am trying to explode everyone and send them a message but it is not working. Here is the code.
Serverside:
local gui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local pp = game:GetService("ProximityPromptService")
local phone = game:GetService("ReplicatedStorage")
local world = game.Workspace
local function pptrig (obj, ply)
for i,v in pairs(game.Players:GetChildren()) do
local player = world:FindFirstChild(v.Name)
local nuke = Instance.new("Explosion", world)
nuke.BlastRadius = 0.9
nuke.BlastPressure = 1000000
nuke.Position = player.HumanoidRootPart.Position
print("run")
phone.Exploded:FireClient(v, v.Name, ply.Name)
end
end
pp.PromptTriggered:Connect(pptrig)
Clientside:
local phone = game:GetService("ReplicatedStorage")
local gui = game:GetService("StarterGui")
gui:SetCore("test", {Text = "Ran"})
local function humiliation(me, ply)
gui:SetCore("test", {Text = "Ran"})
if ply ~= me then
gui:SetCore("Humiliation", {
Title = "Exploded!",
Text = "You have been exploded by "..ply..".",
Duration = 10,
})
elseif ply == me then
gui:SetCore("Humiliation", {
Title = "Exploded!",
Text = "You, "..me..", exploded yourself."
})
end
end
phone.Exploded.OnClientEvent:Connect(humiliation())
When I try to run it it gives me this:
StarterGui:SetCore must be called from a local script. (x2) - Studio
Players.GoldenRStar.PlayerGui.Script:19: attempt to concatenate nil with string - Server - Script:19
Exploded is a remote event and I wanted to Explode everyone and send them a message:
"You have been exploded by GuyThatPressesButtons." and
"You, GuyThatPressesButtons, exploded yourself."
this program tries to loop though every player, explodes them and sends them a fire from the remote event Exploded, then a client script catches it and processes it acordingly.
on the fire, it is sent the players name: v.Name and the player that pressed the button, on the client script both values are nil.

Attempt to call a nil value when changing an IntValue for leaderstats

I'm trying to make an admin panel, everytime I want to change the value with a username
Code:
leaderstats Script;
--// Set up folder
local AdminModule = require(game:GetService('ServerScriptService').leaderstats.MainModule)
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new('Folder', plr)
leaderstats.Name = 'leaderstats'
local Playtime = Instance.new('IntValue', leaderstats)
Playtime.Name = 'Playtime'
end)
AdminModule.GivePoints('happy_speler', 500)
MainModule:
local module = {
GivePoints = function(plr, amount)
plr:WaitForChild('leaderstats'):WaitForChild('Playtime').Value = amount
end,
}
return module
The error is telling you that you have tried to call a function that doesn't exist.
Looking at the AdminModule.GivePoints function, it looks like it expects a Player object for the plr argument, but you have passed in a string. The string library does not have a WaitForChild function, so calling plr:WaitForChild is throwing the error.
The way to fix this is to properly pass in a Player object :
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local AdminModule = require(ServerScriptService.leaderstats.MainModule)
Players.PlayerAdded:Connect( function(plr)
local leaderstats = Instance.new('Folder', plr)
leaderstats.Name = 'leaderstats'
local Playtime = Instance.new('IntValue', leaderstats)
Playtime.Name = 'Playtime'
if plr.Name == 'happy_speler' then
AdminModule.GivePoints(plr, 500)
end
end)

attempt to index nil with 'LoadCharacter'

When trying to respawn the player with plr:LoadCharacter() it just gives me:
attempt to index nil with 'LoadCharacter' & I tried multiple ways to do it such as Player:LoadCharacter() or is there a more efficient way to kill/respawn the player?
--Declared Boolean Global variable
_G.TimerStart = false
--Local Paths to Objeccts
local label = game.StarterGui.TimerGUI.Timer
-- Get Service Variables
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer
--Get Children
local Child = game.Players:GetChildren()
-- Wait for Child Variables
local TimeCountdown = ReplicatedStorage:WaitForChild("Timer")
local timerevent = ReplicatedStorage:WaitForChild("Timer")
local function Thieves(Players)
if _G.TimerStart == false then
for i,v in pairs(game.Teams.Thieves:GetPlayers()) do
game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
wait(2)
plr:LoadCharacter()
wait(7)
timerAmount = 120
end
for i,v in pairs(game.Teams.Police:GetPlayers()) do
game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
wait(2)
plr:LoadCharacter()
wait(7)
timerAmount = 120
end
end
end
In this case plr is a nil value. So plr:LoadCharacter() is not allowed as it does not make any sense.
local plr = game:GetService("Players").LocalPlayer
is the reason.
So refer to this manual page: https://developer.roblox.com/en-us/api-reference/property/Players/LocalPlayer
Maybe this helps:
Loading GUIs When creating loading GUIs using ReplicatedFirst, sometimes a LocalScript can run before the LocalPlayer is available.
In this case, you should yield until it becomes available by using
Instance:GetPropertyChangedSignal
local Players = game:GetService("Players")
-- Below: access Players.LocalPlayer; if it is nil, we'll wait for it using GetPropertyChangedSignal.
local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()

Resetting a script after game round?

When the game timer ends it kills the players & resets the teams and sends them to spawn to choose a Team again... idk how to reset the script to start from the beginning and have reset all the values and functions called... I tried making a copy of the script and destroy the current one with script:Destroy() but doesn't work & continues with the same function so breaks my game when the players choose the teams again & respawn.
-- Get Service Variables
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer
-- Wait for Child Variables
local TeamResetter = game.ReplicatedStorage.TeamResetter
local TimeCountdown = ReplicatedStorage:WaitForChild("Timer")
--Scripts Resets the entire script after GameTime is up
local function ResetGame(Player,Teams)
local copy = script:Clone()
copy.Parent = script.Parent
script:Destroy()
end
-- Destroy Gate when thieves touch it
game.Workspace.CarGate4.GateRod.Touched:Connect(function(hit,Player)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
local n = hit.Parent
local p = game.Players:FindFirstChild(n.Name)
if p.Team.Name == "Thieves" then
game.Workspace.CarGate4.GateRod:Destroy()
end
end
end)
--Thieves function for winning
--[[humanoid.Seated:Connect(function(active,currentSeat, Player, Team)
if active then
if currentSeat.Name == "DriveSeat" then
if Player.TeamColor == game.Teams.Thieves.TeamColor then
game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
end
end
end
end)]]
local function PoliceWinReset(Player,Team)
game.StarterGui.PoliceWinScreen.Frame.TextLabel.Script.Disabled = false
wait(2)
for i,v in pairs(game.Teams.Thieves:GetPlayers()) do
Player.TeamColor = game.Teams.ChoosingTeam.TeamColor
game.StarterGui.ThiefWinScreen.Frame.TextLabel.KillPlayer.Disabled = false
Player.Character:BreakJoints()
game.StarterGui.ChooseTeamGUI.Enabled = true
ResetGame(Player,Teams)
end
for i,v in pairs(game.Teams.Police:GetPlayers()) do
Player.TeamColor = game.Teams.ChoosingTeam.TeamColor
game.StarterGui.PoliceWinScreen.Frame.TextLabel.KillPlayer.Disabled = false
Player.Character:BreakJoints()
game.StarterGui.ChooseTeamGUI.Enabled = true
ResetGame(Player,Teams)
end
end
--Starts Global timer for game after user chooses a team & Police win code
--Resets Player Teams and respawns them back at spawn and have to choose a team again
local function PlayGame(Player, Team)
local timerAmount = 120
local timerText = ""
while timerAmount >= 0 do
TimeCountdown:FireAllClients(timerAmount,timerText)
wait(1)
timerAmount -= 1
if timerAmount == 0 then
PoliceWinReset(Player,Team)
end
end
return timerAmount
end
--Checks wether the user is on the Thieves or Police Teama
local function Thieves_Police(Player, Team)
if Player.TeamColor == game.Teams.Police.TeamColor then
game.StarterGui.ChooseTeamGUI.Enabled = false
game.StarterGui.TimerGUI.Enabled = true
wait(5)
PlayGame(Player, Team)
return Player, Team
elseif Player.TeamColor == game.Teams.Thieves.TeamColor then
game.StarterGui.ChooseTeamGUI.Enabled = false
game.StarterGui.TimerGUI.Enabled = true
wait(5)
PlayGame(Player, Team)
return Player, Team
end
end
--Team Chooser
game.ReplicatedStorage.TeamChooser.OnServerEvent:Connect(function(Player, Team)
local PhysicalTeamColor = Teams:FindFirstChild(Team).TeamColor
Player.TeamColor = PhysicalTeamColor
game.StarterGui.PoliceWinScreen.Enabled = false
game.StarterGui.ThiefWinScreen.Enabled = false
Thieves_Police(Player, Team)
end)
--Gives the Users on the Police Team a Weapon on Spawn
function teamFromColor(color)
for _,t in pairs(game:GetService("Teams"):GetChildren()) do
if t.TeamColor==color then return t end
end
return nil
end
function onSpawned(plr)
local tools = teamFromColor(plr.TeamColor):GetChildren()
for _,c in pairs(tools) do
c:Clone().Parent = plr.Backpack
end
end
function onChanged(prop,plr)
if prop=="Character" then
onSpawned(plr)
end
end
function onAdded(plr)
plr.Changed:connect(function(prop)
onChanged(prop,plr)
end)
end
--Calls the Functions
game.Players.PlayerAdded:connect(onAdded)
You can just wrap the script in a while loop to repeat from the beginning when the round ends. At the end of the loop, right before the end tag, you can reset all the values that are supposed to be reset for the next round.

Roblox Lua cannot access modulescript function

I've been trying to connect a ModuleScript to a server Script, and it's throwing the error: Workspace.Script:6: attempt to call field 'check3' (a nil value)
ModuleScript
local GunStats = {}
local part = workspace:WaitForChild('Mosin'):WaitForChild("Union")
local billboard = workspace:WaitForChild('BillboardPart'):WaitForChild('BillboardGui')
local uis = game:GetService("UserInputService")
local Ekey = Enum.KeyCode.E
local check = false
local start = tick()
local function onpress(action1)
if check then
part.BrickColor = BrickColor.new("Black")
end
end
local function isKeydown(startTime)
return uis:IsKeyDown(startTime) and startTime - tick() <= 5
end
local function Input(input, gameprocessed)
if isKeydown(Ekey) then
print("h")
else
print("n")
end
start = tick()
end
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local humRoot = character:WaitForChild("HumanoidRootPart")
uis.InputBegan:Connect(Input)
GunStats.check3 = function()
while wait() do
if (humRoot.Position - part.Position).Magnitude < 5 then
print("IN RANGE")
check = true
billboard.Enabled = true
repeat wait() until (humRoot.Position - part.Position).Magnitude >= 5
elseif (humRoot.Position - part.Position).Magnitude > 5 and check then
check = false
print("OUT OF")
billboard.Enabled = false
end
end
end
end)
return GunStats
Server Script:
local players = game:GetService("Players")
local serverStorage = game.ServerStorage
local gunStats = require(serverStorage:WaitForChild("ModuleScript"))
game.Players.PlayerAdded:Connect(function(players)
gunStats.check3(players)
end)
This issue is because the gunStats.check3() function isn't defined on the GunStats object until after a player joins. I would restructure your ModuleScript so that GunStats.check3() is defined right away :
--[[ define all your helper functions up here ... ]]
local GunStats = {}
function GunStats.check3(player)
-- access the humanoid
local character = player.Character or player.CharacterAdded:Wait()
local humRoot = character:WaitForChild("HumanoidRootPart")
-- do the checks
end
return GunStats

Resources