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

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

Asked by 4 years ago

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!

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 isaacsoccer 45 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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)
--------------------------------------------
0
im trying to retrieve data though. ill try using remotefunction CHEESEPIZZAPARTYTIME 5 — 4y
Ad

Answer this question