Need help making Corn pickup serverside

I want to say upfront that I have very little experience working with any kind of script. The code I have has come from me watching videos and throwing everything at the script until something works. I am working to learn how to create code but I am not an expert at it. On to the problem.

I have a corn maze game where 7 pieces of corn spawn at random locations.

I am looking to have the corn when it is picked up to show on the scoreboard for all players to see. I THINK I need to put a “remote event” inside of Replicated Storage so that every time a player picks up a piece of corn it shows for all players. Right now I have the corn set up like this
image_2024-04-01_114039177

inside replicated storage is a folder labeled “items” in that folder is where the corn will spawn. Inside the Corn I have the pick up script that technically works, however when the player picks it up the score does not change and the corn or its particles are not destroyed.
this is the working script I cobbled together.

local corng = script.Parent
local canGet = true

corng.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum and canGet then
		canGet = false
		corng.Transparency = 1
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		player.leaderstats.Corn.Value += 1
		corng:Destroy()
	end
end)

Again, the player can pick up the corn. But it throws an error every time. I am not sure what “leaderstats” is even referencing here since I don’t think I need it since I am using my own GUI to show the amount of corn that has been collected?

So, in short what I’m looking for mostly is getting the corn pickup to be serversided to all players, if that’s even what its called? I think I can figure out how to make the particles and mesh to go away but if anyone know how to do that too I would appreciate it regardless. ANY help or point in the right direction is very very appreciated and thank you for taking the time out to read this.

1 Like

That’s a conversion of PlayerList to leaderboard showing stats of every player, assuming you don’t have that folder inside, They are created each time a player joins by PlayerAdded event in another script, and since you don’t want it to show on leaderstats, you could use events to do the update each time a player gets the corn and show the amount of corns in client.

There’s plenty ways you can do but my approach is simply storing that number in a value object being IntValue in the main game script, should be hidden in a ServerScriptService and fire that using BindableEvent (because it’s obvious that we cannot fire the event in the same type using RemoteEvent)

after the event received the call, add the number there and fire the amount of number collected to the client so it can change the numbers

so in a script form, it should be like this

--this one in a corn script
local playerSV = game:GetService("Players")
local replicatclient = game:GetService("ReplicatedStorage")
local replicatserver = game:GetService("ServerScriptService")
local bindable_src = replicatclient.ypur_path_to_bindable_event_here

local corn_par = script.Parent
local deb = true

corn_par.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid and deb then
		deb = false
		corn_par.Transparency = 1
		  bindable_src:Fire()
		corn_par:Destroy()
	end
end)

-- somewhere in a server script service
local replicatclient = game:GetService("ReplicatedStorage")
local replicatserver = game:GetService("ServerScriptService")
local bindable_src = replicatclient.ypur_path_to_bindable_event_here
local corn_num = script.corn_num --assuming IntValue is in the script, you can put it in anywhere you want but make sure to change the path of this
local remote_srv = replicatclient.path_to_remote

bindable_src.Event:Connect(function()
	corn_num.Value += 1 --server will assign it
	remote_srv:FireAllClients(corn_num.Value) --because local script cannot access it, This should be contained in the arg
end)

--In a ScreenGUI as a LocalScript
local replicatclient = game:GetService("ReplicatedStorage")
local remote_srv = replicatclient.path_to_remote
local GUI = script.Parent
local txt = GUI.path_to_textlabel --to wherever it is

remote_srv.OnClientEvent:Connect(function(num) 
	txt.Text = num --changes the text
end)

hopefully this logic should help you figure without using leaderstats

Thank you for the speedy reply. I’m having a bit of trouble figuring out what I need to put in the server script though. Im getting an error on this line here,

remote_srv:FireAllClients(corn_num.Value) --because local script cannot access it, This should be contained in the arg

I moved this script to a serverscript on its own. I don’t have any bindable events yet because I’m only working with the corn right now, so I’m not even sure what events need to run together?

Oh i realized that there’s no brackets at the “end” for that bindable events, was typing this at midnight on a phone, ive modified a bit so this should work now

--this one on server script service
local replicatclient = game:GetService("ReplicatedStorage")
local replicatserver = game:GetService("ServerScriptService")
local bindable_src = replicatclient.ypur_path_to_bindable_event_here
local corn_num = script.corn_num --assuming IntValue is in the script, you can put it in anywhere you want but make sure to change the path of this
local remote_srv = replicatclient.path_to_remote

bindable_src.Event:Connect(function()
	corn_num.Value += 1 --server will assign it
	remote_srv:FireAllClients(corn_num.Value) --because local script cannot access it, This should be contained in the arg
end) --this is where it happens, i forgot to add a lil close bracket since it's obv an event

also if you’re still stuck on putting something, here’s how it would look like in explorer
image

as said earlier, it’ll need 2 events object and that one would be for telling the server to fire remote to all clients after received a communications from other server script instead of re-enabling script to restart the script


for BindableEvent, you don’t need to store anything in the bracket so only RemoteEvent later to get the data from the Remote it fires, the one that ive put in the FireAllClients is an integer value on Server and it cannot be accessed by LocalScript so this one exists to send that to all clients, notifying that something has changed.

annnnnnnnd, file here to sandboxin around incase you’re still struggling to make it, hopefully this structure should help learning how things work :+1:
corn_game_demonstration.rbxl (105.5 KB)

1 Like

Thank you thank you so much! I appreciate that you gave me a file to reference from while I’m trying to learn this! Everything works perfectly, the only problem I’m really having is that when the player dies, their score is not saved and resets back to zero. Otherwise everything is almost perfect.

It might be something I’ve done?

Edit: Upon testing it a bit more, it turns out the “value” is sill there, as when I pick up a new piece of corn the number fixes itself with what is stored in the server, I just need to make a line of code when the player respawns that tells it to update to the current value.

You can disable ResetOnSpawn on ScreenGUI.

1 Like

Yes! That was the issue, not sure how I didn’t notice that before! Again thank you so much for all your help! Let me know if there’s anything I can do to repay you, you don’t know how long I’ve been trying to find a way to make this work, I truly appreciate it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.