I want to make a system where when you catch a fish, it shows how many times you caught it, but I can’t figure out what needs to be done for the program to read it, I wanted to try intValue but I didn’t understand how it could be used.
The video:
The code:
local fish = {'Cod','Salmon','Shark','Mullet'}
game.ReplicatedStorage.Fishing.OnClientEvent:Connect(function()
script.Parent.Visible = true
local selection = math.random(1,#fish)
local picked_value = fish[selection]
script.Parent.TextLabel.Text = picked_value
local price = math.random(20,40)
script.Parent.PriceDesc.Text = ('$'..price)
game.ReplicatedStorage.Fishing:FireServer(price)
wait(2)
script.Parent.Visible = false
local staticmenu = script.Parent.Parent.Parent.StaticMenu
local static = staticmenu.Static
for _, i in pairs(static:GetChildren()) do
if picked_value == i.Name then
--I can't figure out what to put here
end
end
end)
If you know how to make the program count how many times you caught a fish, help me.
(If you need more information please ask)
You could parent a folder to the player that serves as an inventory, each with a NumberValue that is the name of a fish (this should be done in a ServerScript when the game starts). If you want to save the data, you could save the fish names under the player’s data and then set their values to the number of times they have been caught?
Either way, it would work something like this in your LocalScript:
game.ReplicatedStorage.Fishing.OnClientEvent:Connect(function()
local player = game:GetService("Players").LocalPlayer
-- get the inventory stored as intValues
local playerInventoryFolder = player:FindFirstChild("inventoryFolder")
script.Parent.Visible = true
local selection = math.random(1,#fish)
local picked_value = fish[selection]
script.Parent.TextLabel.Text = picked_value
local price = math.random(20,40)
script.Parent.PriceDesc.Text = ('$'..price)
game.ReplicatedStorage.Fishing:FireServer(price)
wait(2)
script.Parent.Visible = false
local staticmenu = script.Parent.Parent.Parent.StaticMenu
local static = staticmenu.Static
for _, i in pairs(static:GetChildren()) do
if picked_value == i.Name then
--access the number of that fish
local pickedQuantity = playerInventoryFolder:FindFirstChild(picked_value)
pickedQuantity.Value = pickedQuantity.Value + 1 -- numberValue in the folder
-- then set the text of the label that shows the number to that value using tostring()
end
end
end)
The intValues should be the same name as the fish types. When you create them, it will look something like this:
local fishInventory = Instance.new("Folder")
fishInventory.Parent = player
fishInventory.Name = "fishInventory"
local mulletValue = Instance.new("NumberValue")
mulletValue.Name = "Mullet"
mulletValue.Value = 0
mulletValue.Parent = fishInventory
-- repeat for other fish: Cod, Salmon, Shark
If you have data saving you could make a loop instead that does this for all the fish in the player’s inventory and sets the value to whatever is saved
local Players = game:GetService("Players")
local fish = {'Cod', 'Salmon', 'Shark', 'Mullet'}
local function onPlayerAdded(player)
local fishFolder = Instance.new("Folder")
fishFolder.Name = "FishData"
fishFolder.Parent = player
for _, fishType in ipairs(fish) do
local fishValue = Instance.new("NumberValue")
fishValue.Name = fishType
fishValue.Value = 0
fishValue.Parent = fishFolder
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in ipairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
local script in StarterPlayerScripts
task.wait(3) -- pausing for server script to set up.. then a test
local player=game:GetService("Players").LocalPlayer
local fish = {'Cod','Salmon','Shark','Mullet'}
local function incrementFishCount(player, fishType)
local fishData = player:WaitForChild("FishData")
local fishValue = fishData:WaitForChild(fishType)
fishValue.Value = fishValue.Value + 1
end
incrementFishCount(player, "Salmon")
feel free to try other ways … Attempting to place a folder and use it to keep track of fish …
100% not secure as is.
-- References to UI elements
local player = game.Players.LocalPlayer
local gui = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
local fishButton = gui:WaitForChild("FishButton")
local resultLabel = gui:WaitForChild("ResultLabel")
-- Fishing rod settings
local fishingRod = nil
local isFishing = false
-- Function to start fishing
local function startFishing()
if isFishing then return end -- Prevent multiple fishing actions at once
isFishing = true
-- Equip fishing rod (This is a placeholder, implement your own logic)
if not fishingRod then
fishingRod = Instance.new("Tool")
fishingRod.Name = "FishingRod"
fishingRod.Parent = player.Backpack
end
player.Character.Humanoid:EquipTool(fishingRod)
-- Start fishing animation or logic
resultLabel.Text = "Fishing..."
wait(2) -- Simulate fishing time
-- Determine if the player catches a fish
local caughtFish = math.random() > 0.5
if caughtFish then
resultLabel.Text = "You caught a fish!"
else
resultLabel.Text = "No fish caught. Try again!"
end
-- Unequip fishing rod
player.Character.Humanoid:UnequipTools()
-- Allow fishing again
isFishing = false
end
-- Connect the button to start fishing
fishButton.MouseButton1Click:Connect(startFishing)