✓ Accepted answer
You would have to use Remote Events on this.
###Also, I am assuming that you are using a ServerScript for which you wouldn't want to use LocalPlayer and vice versa.
Remote Events are Roblox Instances that allows you to communicate with two different scripts. Some of these actions can only be performed by server and some can only be done with client. When creating a game, you need to identify which action is suitable for server and client.
How to implement Remote Events?
- RightClick on
ReplicatedStorage
- Select
RemoteEvent
- Change the Event to your desire.
Now we move into coding.
What you want to do is to create a 2 different scripts for this. First script, ServerScript is inside the part you want to touch.
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TouchPart = Workspace:WaitForChild("Part1")
local AddValueEvent = ReplicatedStorage:WaitForChild("AddValueEvent")
local function FireServerWhenTouched (hit, plr)
if hit.Parent:FindFirstChild("Humanoid") then
TouchPart:Destroy()
AddValueEvent:FireClient(plr)
end
end
TouchPart.Touched:Connect(FireServerWhenTouched)
##Explanation
What I am doing here is that I am checking if the Player's Humanoid is present. If it is, then the TouchPart will be Destroyed along with the AddValueEvent being fired. You may notice that the Event has a argument. This is necessary since we are communicating with the LocalScript from ServerScript. Hence we use a argument.
Now the LocalScript is the one making all the changes.
####Place this in StarterPlayerScripts
local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AddValueEvent = ReplicatedStorage:WaitForChild("AddValueEvent")
local ShowGui = PlayerGui:WaitForChild("ShowGui")
local ValueShower = ShowGui:WaitForChild("ValueShower")
local Backpack = Player:WaitForChild("Backpack")
local Stats = Backpack:WaitForChild("Stats")
local FoodValue = Stats:WaitForChild("FoodValue")
local function ChangeMessageWhenFired ()
FoodValue.Value = 1
ValueShower.Text = FoodValue.Value
end
AddValueEvent.OnClientEvent:Connect(ChangeMessageWhenFired)
What we are doing here is that when the AddValueEvent is fired, the function will begin to process. Then the rest is self-explanatory.
#####Have a lovely day of coding.
1 comment
Awesome, thanks for writing it all out, it really helps!