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

Why Doesn't my Script Work? I need a part to destroy and add a value to player when touched!

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)

1 answer

✓ 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.

What are Remote Events?

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.

-- 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)

##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

-- 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.

#####Have a lovely day of coding.

1 comment

0

Awesome, thanks for writing it all out, it really helps!

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