I'm making a survival game, and I want to be able to give the player wood when they break down a tree. I already have that entire system set up except for the wood giving part. I won't include any code since nothing will affect anything I'm asking about, but I would just like to know how I could change the value of an IntValue in the player after breaking a tree in the workspace?
I made you a script for that to work.
If you want the part with remote events (server-sided):
First, you need to put an RemoteEvent
into ReplicatedStorage
.
Rename the RemoteEvent
to BreakWood
You will also need some scripts. Put LocalScript into StarterPlayer > StarterPlayerScripts
.
Name it WoodBreakingClient
local ReplicatedStorage = game:GetService("ReplicatedStorage") local UserInputService = game:GetService("UserInputService") local player = game.Players.LocalPlayer local cursor = player:GetMouse() UserInputService.InputBegan:Connect(function(input) if input.UserInputType == Enum.UserInputType.MouseButton1 and cursor.Target.Name == "Wood" then -- Check if user left clicks on a wood piece player:FindFirstChild("Wood").Value += 1 -- Add 1 to the player's wood value ReplicatedStorage:FindFirstChild("BreakWood"):FireServer(cursor.Target) -- Fires server so the wood breaks for everyone end end)
Now the server script (in ServerScriptService):
We'll call it WoodBreakingServer
local ReplicatedStorage = game:GetService("ReplicatedStorage") ReplicatedStorage:FindFirstChild("BreakWood").OnServerEvent:Connect(function(player, target) -- Runs when BreakWood is fired target:Destroy() -- Destroys wood (it applies to everyone) end)
Client-sided (it will apply only to you):
Do the same as above. Again, put a LocalScript into StarterPlayer > StarterPlayerScripts
.
Name it same as above, WoodBreakingClient
But now, we're gonna use different code.
local UserInputService = game:GetService("UserInputService") local player = game.Players.LocalPlayer local cursor = player:GetMouse() UserInputService.InputBegan:Connect(function(input) if input.UserInputType == Enum.UserInputType.MouseButton1 and cursor.Target.Name == "Wood" then player:FindFirstChild("Wood").Value += 1 cursor.Target:Destroy() end end)
This is all I could tell you. Hope this helps!