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!
No worries, I'll help you out. I'll write a basic template for what you need to do:
Create a "Remote Event" instance and drop it inside of the "ReplicatedStorage".
Name it whatever you want.
Create a "Script" instance and drop it inside of the "ServerScriptService".
Name the script whatever you want.
Put a "Local Script" inside of the desired part in the "Workspace".
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) --------------------------------------------