Plant growing scirpt not working?

So I have a plant growing script that does not work, what am I doing wrong here? No output errors.

local Player = game.Players.LocalPlayer
local Character = Player.Character
local mouse = Player:GetMouse()
local uis = game:GetService("UserInputService")

uis.InputBegan:connect(function(inst)
	print("began input")
    if mouse.Target and inst.KeyCode == Enum.KeyCode.F and mouse.Target.Name == 'PlantDetector' and mouse.Target.Parent.Name == 'Plant1' and mouse.Target.Finished == false and Player.Character.Seed and (Player.Character.HumanoidRootPart.Position - mouse.Hit.p).magnitude <= 10 then
	print("Finished")
	local Plant = mouse.Target
    game.ReplicatedStorage.GrowPlant:FireServer()-- stop code if it isnt false
    print("Fired")
end
end)

you used userinputservice, don’t use that, use mouse.Button1Down:Connect(function(inst)

I want it to be a keybind though.

then have it so there is an if to detect if the mouse is pressed down: if mouse.Button1Down then(add to the OLD script, not the new one I said to do)

I am not following? I apologize.

1 Like

UserInputService should be used over Mouse members where possible as the Mouse API is considered legacy and superseded by UserInputService and ContextActionService. This is not the issue.

mouse.Button1Down will always just be an RBXScriptSignal and thus pass the if statement check.

3 Likes

actuallly, I found out a way to use mouse in inputservice: if input.KeyCode == Enum.UserInputType.MouseButton1 then

1 Like
local Player = game.Players.LocalPlayer
local Character = Player.Character
local mouse = Player:GetMouse()
local uis = game:GetService("UserInputService")

uis.InputBegan:connect(function(inst)
	print("began input")
    if if input.KeyCode == Enum.UserInputType.MouseButton1 and inst.KeyCode == Enum.KeyCode.F and mouse.Target.Name == 'PlantDetector' and mouse.Target.Parent.Name == 'Plant1' and mouse.Target.Finished == false and Player.Character.Seed and (Player.Character.HumanoidRootPart.Position - mouse.Hit.p).magnitude <= 10 then
	print("Finished")
	local Plant = mouse.Target
    game.ReplicatedStorage.GrowPlant:FireServer()-- stop code if it isnt false
    print("Fired")
end
end)

the script is probably not working because of the about of ifs in one line

Is Finished a value object? If so, change this code to mouse.Target.Finished.Value == false. Finished itself would always just be the Roblox value object. The value of the object is stored in its Value property.

You are correct, also, the “input” is not like, known, so its orange.

This will always evaluate to false as the input object’s key code cannot be two things at once.

1 Like

So far, this thread is very vague. Can you elaborate more please?

How is the script not working? Is anything happening at all? Or is something unexpected happening?

Also, because some of your script is server-sided (using remote events), we are only seeing half of your script. Please post the event handler on the server.

Finally, are any of your print statements printing to the console?

Only the “began input” is going threw, nothing else, thats why im leaving the other script out right now.

That means that something in your if statement is evaluating to false. Make sure all the checks in the if statement are definitely true. Maybe try printing them?

Also, your if statement is very long and has many and joins. Maybe break it up into two if statements, for readability’s sake.

OK. Try changing mouse.Target.Finished == false to mouse.Target.Finished.Value == false inside the if statement condition.

I have done that, but im still on the “input” not being known and orange underlined.

I don’t even see input in your original code. Are you using TOP_Crundee123’s version? If so, don’t, as unfortunately it doesn’t fix the issue and makes the if statement never pass.

You’re not passing plant on the client. You need to add it as an argument to the line reading :

Basically the error says that that variable is nil (hasn’t been set).

So I edited my script a little bit to try and make it so when the plant is done growing, you can grab it using the same F keybind, and I so put this together, and its not working and is spamming the output with the began input and other print, any help?

local Player = game.Players.LocalPlayer
local Character = Player.Character
local mouse = Player:GetMouse()
local uis = game:GetService("UserInputService")

uis.InputBegan:connect(function(inst)
	print("began input")
    if mouse.Target and inst.KeyCode == Enum.KeyCode.F and mouse.Target.Finished.Value == false and mouse.Target.Name == 'PlantDetector' and mouse.Target.Parent.Name == 'Plant1' and Player.Character.Seed and (Player.Character.HumanoidRootPart.Position - mouse.Hit.p).magnitude <= 10 then
	print("Finished")
	local Plant = mouse.Target
    game.ReplicatedStorage.GrowPlant:FireServer(Plant)-- stop code if it isnt false
    print("Fired")
else
	print("began input (to get stuff)")
    if mouse.Target and inst.KeyCode == Enum.KeyCode.F and mouse.Target.Finished.Value == true and mouse.Target.Name == 'PlantDetector' and mouse.Target.Parent.Name == 'Plant1' and Player.Character.Seed and (Player.Character.HumanoidRootPart.Position - mouse.Hit.p).magnitude <= 10 then
	print("Finished")
    game.ReplicatedStorage.GivePlant:FireServer()
    print("Fired")
end
end
end)

First of all, can you check that the server is correctly receiving the event by adding a print statement on the server.

Second, is the if statement working correctly and printing the second print after you press F once? So, for example, does the output look like this:

Began input
Finished
Fired
began input (to get stuff)
...

Press the F key twice and see if it looks like that.

Finally, can we please see the server script? There might be a problem there. I have a suspicion that the finished value in the target isn’t being set.

1 Like