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

How could I use remote events to give the player items?

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?

1 answer

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!

2 comments

0

Well thank you, it really does help, but not 100%. I actually needed an event for firing the client, and not the server, I'm so sorry for not specifying,my dumb mind thought everyone would know apparently. All I need to know is what I fire the client to though, since I don't know how to get the player on a serverscript. Again, I'm so sorry for not specifying.

0

If you want client-sided, it won't apply to everyone since the event fires only for you, the one that breaks the tree. Server-sided is better because it applies to everyone and looks more realistic.

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