I am making a script where when a player touches a certain part, the part disappears and adds 1 to a number value inside the player. I get no errors, just nothing happens. Any I Idea? Thank you -TheFierceWaffle
local play = game.Players.LocalPlayer local startgui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui") local food = script.Parent local foodam = play.Backpack.InvStats.FoodAmm local foodamv = startgui.ScreenGui.InStats.FoodAmm local hunv = startgui.ScreenGui.Stats.HungerValue local hunva = play.Backpack.Stats.Hunger food.Touched:connect(function(onTouched) food:Destroy() foodam.Value = (foodam.Value + 1) foodamv.Text = foodam.Value end)
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.
ReplicatedStorage
RemoteEvent
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.
-- Declaration Section -- //Game Services local Workspace = game:GetService("Workspace") local ReplicatedStorage = game:GetService("ReplicatedStorage") --//Items local TouchPart = Workspace:WaitForChild("Part1") local AddValueEvent = ReplicatedStorage:WaitForChild("AddValueEvent") -- Processing Section local function FireServerWhenTouched (hit, plr) if hit.Parent:FindFirstChild("Humanoid") then TouchPart:Destroy() AddValueEvent:FireClient(plr) end end -- Connecting Section TouchPart.Touched:Connect(FireServerWhenTouched)
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.
StarterPlayerScripts
-- Declaration Section -- //Game Services local Player = game:GetService("Players").LocalPlayer local PlayerGui = Player:WaitForChild("PlayerGui") local ReplicatedStorage = game:GetService("ReplicatedStorage") -- //Remote Event local AddValueEvent = ReplicatedStorage:WaitForChild("AddValueEvent") -- //PlayerGui Precedence local ShowGui = PlayerGui:WaitForChild("ShowGui") local ValueShower = ShowGui:WaitForChild("ValueShower") -- //PlayerBackpack local Backpack = Player:WaitForChild("Backpack") local Stats = Backpack:WaitForChild("Stats") local FoodValue = Stats:WaitForChild("FoodValue") -- Processing Section local function ChangeMessageWhenFired () FoodValue.Value = 1 ValueShower.Text = FoodValue.Value end -- Connecting Section 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.