Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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 :)

5 comments

0

ServerScriptService.Script:8: attempt to index global 'P' (a nil value)

0

I changed the script, I forgot to add 'P' as an argument on FireServer() function. LocalScript, line 10 has been changed.

0

The Player isn't passed as an argument to FireServer. and connect is deprecated, you should be using Connect.

0

Hm, P is still nil

0

Actually, I fixed it.

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,

This discussion is preserved as it was. New answers, comments, and votes are closed.