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)
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.
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.
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?
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.
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.
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.