I don't know how to make these Scripts into LocalScripts

Hello! I’m trying to convert two scripts into local ones, since they both run on the server when I want them to run locally.

The issue is that, well, I can’t really get it to work, as I don’t know how. Sorry if this topic seems kind of vague, I just can’t really figure anything out right now.
Scripts:

--corn gen script
print("Running")
local RepStorage = game:GetService("ReplicatedStorage")
local cornamount = RepStorage:WaitForChild("cornamount")
local module = require(RepStorage:WaitForChild("ModuleScript"))
local dirt = game.Workspace.dirt
local corn = RepStorage:WaitForChild("corn")


local function addcorn()
	wait(module.waitTime)
	local corn2 = corn:Clone()
	corn2.Parent = game.Workspace
	corn2.Transparency = 0

	local randomVal = math.random(1,2)

	if randomVal == 1 then
		local x = math.random(-37,37)
		local z = math.random(-37,37)

		corn2.Position = dirt.Position + Vector3.new(x, dirt.Size.Y, z)
	elseif randomVal == 2 then
		local x = math.random(-37,37)
		local z = math.random(-37,37)

		corn2.Position = dirt.Position - Vector3.new(x, -dirt.Size.Y, z)
	end
	cornamount.Value = cornamount.Value + 1
end

while true do
	if cornamount.Value <= module.CornCap then
		addcorn()
	else
		wait()
	end
end
--Basically what happens if the corn is touched
local Players = game:GetService("Players")
local corn = script.Parent
local RepStorage = game:GetService("ReplicatedStorage")
local SGui = game:GetService("StarterGui")
local cornamount = RepStorage:WaitForChild("cornamount")
local cornamountLabel = SGui.GameStats.Frame.Frame:WaitForChild("cornamountlabel")

local module = require(RepStorage:WaitForChild("ModuleScript"))
corn.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		corn:Destroy()
		cornamount.Value = cornamount.Value - 1
		local leaderstats = player.leaderstats
		local cornStat = leaderstats:FindFirstChild("corn")
		if cornStat then
			cornStat.Value = cornStat.Value + module.cornVal
		end
	end
end)

Thanks for any help!

What is the problem that happened with this script? Please be clear on why it is not working.

Also, first thing first, All the contents in StarterGui will be automatically place in game.Players.LocalPlayer.PlayerGui. If you want to use it, please refer to this instead.

1 Like

Okay, so the first script might be working fine as a localscript, but I’m still not fully sure. The 2nd script doesn’t work as a localscript because it never even runs.

What’s the Purpose of doing this if they lead to a similar Result?
(if statements btw)

Randomly checking if the thing will generate with the dirt’s position plus a random vector3, or minus a random vector3

Where is it located? What is its parent?

Which one? Kinda hard to tell with the vague phrasing

The… one that doesn’t work. Where is it located?

I assume that your second script is located somewhere in Workspace
Local scripts will never run on Workspace.

According to the Roblox creator documentation:

A LocalScript will only run Lua code if it is a descendant of one of the following objects:

You should also notice that you need to change a player’s value through the server in case you want to datastore it (since DataStoreService only runs on server).

If that’s not the issue, then just debug your 2nd script using print() on different states of your code!

1 Like

The 2nd script is inside of a union located in replicated storage

Debug your script using print() on every state it has
(I’m talking about after if statements or inside functions or whatever)

1 Like

Sorry for the long response!
Like I said, the 2nd script is located inside of a union located in replicated storage

Your script cant be there and also run.

Check this:

A LocalScript will only run Lua code if it is a descendant of one of the following objects:

1 Like

Ahh, I’m pretty sure i found the root of the problem. Since the part is created with a localscript, I can’t run a .touched event because it only works on server scripts or if a part was created by the server
The first script is working fine, though.
@Corruptux @Jacky2804 @kokirimagic

1 Like

You can fire a .touchedevent in a local script.
Btw you can’t change a leaderstats value locally, because DatastoreService is server-sided.

1 Like

I also assume that the script stops at this line.
Can you add a print() before this line so I can tell if that’s true or not?

I’ve already 100% confirmed it does because of another issue i’m having, said in a different topic of mine

1 Like

Issue has been fixed. I combined both the scripts together to make them work

--corn handler localscript
print("Running")
local RepStorage = game:GetService("ReplicatedStorage")
local cornamount = RepStorage:WaitForChild("cornamount")
local module = require(RepStorage:WaitForChild("ModuleScript"))
local dirt = game.Workspace.dirt
local corn = RepStorage:WaitForChild("corn")
local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local CornHarvestEvent = RepStorage:WaitForChild("CornHarvestEvent")

local function addcorn()
	wait(module.waitTime)
	local corn2 = corn:Clone()
	corn2.Parent = game.Workspace:WaitForChild("Corns")
	corn2.Transparency = 0

	local randomVal = math.random(1,2)

	if randomVal == 1 then
		local x = math.random(-37,37)
		local z = math.random(-37,37)

		corn2.Position = dirt.Position + Vector3.new(x, dirt.Size.Y, z)
	elseif randomVal == 2 then
		local x = math.random(-37,37)
		local z = math.random(-37,37)

		corn2.Position = dirt.Position - Vector3.new(x, -dirt.Size.Y, z)
	end
	cornamount.Value = cornamount.Value + 1
	corn2.Touched:Connect(function(hit)
		corn2:Destroy()
		print("Touched")
		print("Corn touched by " .. plr.Name)
		cornamount.Value = cornamount.Value - 1
		local leaderstats = plr.leaderstats
		local cornStat = leaderstats:FindFirstChild("corn")
		CornHarvestEvent:FireServer(plr, cornStat, corn)
		print("Event fired")
	end)
	
end
while true do
	if cornamount.Value <= module.CornCap then
		addcorn()
	else
		wait()
	end
end

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