Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make this Client sided script into a Server sided script?

Asked by 5 years ago

I have a script here, it is quite short and self explanatory. It is currently a Client sided function under a LocalScript. However, the purpose I am using it for, requires it to be Server sided instead. How would I go about making this server sided?

Any help would be greatly appreciated. Thank you!

wait(1)

local Player = workspace:FindFirstChild(script.Parent.Name)
local P = game.Players.LocalPlayer

if not Player:FindFirstChild("Team") then
    local value = Instance.new("StringValue")
    value.Name = 'TEAM'
    value.Parent = Player
    value.Value = P.Team.Name
end

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You can't make client-sided script a server-sided script. However, you can use Remote Event or Remote Function to let LocalScript and Script communicate with each other.

I'll show you how to use Remote Event. It sends a signal to another script.

LocalScript:

wait(1)

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local addValue = ReplicatedStorage:WaitForChild(YOUR REMOTE EVENT'S NAME)

local Player = workspace:FindFirstChild(script.Parent.Name)
local P = game.Players.LocalPlayer

if not Player:FindFirstChild("Team") then
    addValue:FireServer(P)
end

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local addValue = ReplicatedStorage:WaitForChild(YOUR REMOTE EVENT'S NAME)

function addingValue(player)
    local value = Instance.new("StringValue")
    value.Name = 'TEAM'
    value.Parent = Player
    value.Value = P.Team.Name
end

addValue.OnServerEvent:connect(addingValue)

I hope I helped you :)

0
ServerScriptService.Script:8: attempt to index global 'P' (a nil value) GoddessHella 4 — 5y
0
I changed the script, I forgot to add 'P' as an argument on FireServer() function. LocalScript, line 10 has been changed. KrzysiekRoblox 50 — 5y
0
The Player isn't passed as an argument to FireServer. and connect is deprecated, you should be using Connect. User#19524 175 — 5y
0
Hm, P is still nil GoddessHella 4 — 5y
0
Actually, I fixed it. GoddessHella 4 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

This is the updated version, it's mostly @KrzysiekRoblox's code, but I did have to do some tweaking. I still gave him credit for basically giving me it. LocalScript:

wait(1)

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local addValue = ReplicatedStorage:WaitForChild("UpdateTeam")

local Player = workspace:FindFirstChild(script.Parent.Name)

if not Player:FindFirstChild("Team") then
    addValue:FireServer()
end

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local addValue = ReplicatedStorage:WaitForChild("UpdateTeam")

function addingValue(player)
    local value = Instance.new("StringValue")
    value.Name = 'TEAM'
    value.Parent = player.character
    value.Value = player.Team.Name
end

addValue.OnServerEvent:Connect(addingValue)

Thanks,

Answer this question