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

How should I access playerdata from a part in a workspace?

I'm trying to make something that involves using playerdata stored inside the player, but client side things cant access server side instances. I think I have to use remoteevents, but I dont know how to pass on data unsing remote events and when to fire them.

TL;DR: If i were to get a value from inside a player from a script inside a part in the workspace, what would be the best way to do it?

Thanks!

1 comment

0

Hopefully my answer semi-explains what you're looking for. Most of it is up to you, such as how the part needs to fire the event and such. It doesn't NEED to be a touched event. In the same case, you don't have to use levels and experience points and what not

1 answer

No worries, I'll help you out. I'll write a basic template for what you need to do:

  1. Create a "Remote Event" instance and drop it inside of the "ReplicatedStorage".

  2. Name it whatever you want.

  3. Create a "Script" instance and drop it inside of the "ServerScriptService".

  4. Name the script whatever you want.

  5. Put a "Local Script" inside of the desired part in the "Workspace".

  6. Name the local script whatever you want.

--// Inside the local script inside of the part under "Workspace"

--// Services:
local _RS = game:GetService("ReplicatedStorage");

--// References:
local event = _RS:WaitForChild("Event Name Here") 

local part = script.Parent;

--// Variables:
local xpToGive = 5;
local delay = 5;

local debounce = false;

--// Cached Functions:


--// Core:
part.Touched:connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid");

	if not humanoid then
		humanoid = hit.Parent.Parent:FindFirstChild("Humanoid");
	end

	if humanoid then
		
		event:FireServer(xpToGive);

	end
end)

Above is the local script

This is the server script | V

--// Inside the script under "ServerScriptService"

--// Services:
local _RS = game:GetService("ReplicatedStorage");
local _P = game:GetService("Players");

--// References:
local event = _RS:WaitForChild("Event Name Here") 

--// Variables:


--// Cached Functions (cached functions run faster):
local inew = Instance.new;

--// Core:
--[[ Creating the Event's Connection ]]--
event.OnServerEvent:connect(function(player, bonusXp)
	
	local data = player:FindFirstChild("Data");

	if data then
		local exp = data:FindFirstChild("Exp");

		if exp then
			exp.Value = exp.Value + bonusXp;
		end
	end

end)
----------------------------------------------------

--[[ Creating the Player's Data ]]--
_P.PlayerAdded:connect(function(player)
	
	local data = inew("Folder", player);
	data.Name = "Data"; -- Or whatever name you want

	local level = inew("IntValue", data);
	level.Name = "Level";
	level.Value = 1;

	local exp = inew("IntValue", data);
	exp.Name = "Exp";

	level.Changed:connect(function()
		if exp.Value >= level.Value * 10 then
			exp.Value = 0;
			level.Value = level.Value + 1;
		end
	end)

end)
--------------------------------------------

1 comment

0

im trying to retrieve data though. ill try using remotefunction

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