Boat Spawn (Roblox Studio) - roblox

I'm trying(I'm very new) to make a game where you have to attack each other with a boat. There is a button on each island and when you press it, a boat should appear. (this works so far)
But as soon as you want to spawn a new boat it removes everyones boat. I thought it makes sense to use player.id but this still won't work. Does anyone know the solution?
Below you will find the code thats hidden in a part.
Thanks!
CODE:
local cd = script.Parent.ClickDetector
local boat = script.Parent.Parent.Firstboat
local button = script.Parent
local debounce = false
game.Players.PlayerAdded:Connect(function(player)
print(player.UserId)
local plruserid = player.UserId
print(player.Name)
boat.Parent = game.ServerStorage
cd.MouseHoverEnter:Connect(function()
button.Transparency = 0.5
end)
cd.MouseHoverLeave:Connect(function()
button.Transparency = 0
end)
cd.MouseClick:Connect(function()
local SetNameToBoat = plruserid
print (SetNameToBoat)
local oldboat = workspace:FindFirstChild(SetNameToBoat)
if not debounce then
if oldboat then
oldboat : destroy()
end
debounce = true
local NewBoat = boat:clone()
NewBoat.Name = (SetNameToBoat)
NewBoat.Parent = game.Workspace
wait(5)
debounce = false
end
end)
end)

I simplified the code and found the solution:
local cd = workspace.Button:WaitForChild('ClickDetector')
local boat = game:GetService('ServerStorage'):WaitForChild('Boat')
local button = workspace:WaitForChild('Button')
local debounce = false
cd.MouseHoverEnter:Connect(function()
button.Transparency = 0.5
end)
cd.MouseHoverLeave:Connect(function()
button.Transparency = 0
end)
cd.MouseClick:Connect(function(player)
local plruserid = player.UserId
local SetNameToBoat = plruserid
print(SetNameToBoat)
local oldboat = workspace:FindFirstChild(SetNameToBoat)
if not debounce then
if oldboat then
oldboat:destroy()
end
debounce = true
local NewBoat = boat:clone()
NewBoat.Name = SetNameToBoat
NewBoat.Parent = game.Workspace
wait(5)
debounce = false
end
end)

Related

Roblox Functions are running 20+ times

I seem to be having an issue of every time I want to pass a function when an event happens such as a Humanoid.Died() or a BindableEvent it runs the function 20+ times, also adding to an IntegerValue that many times as well. I have tried debounces, BindableEvents, connection:Disconnect(), etc. Is there something I'm doing wrong or missing?
Updated original code:
function module.damageHumanoid(attacker, player, humanoid, damage)
local connection
local debounce = false
connection = humanoid.Died:Connect(function()
if not debounce then
debounce = true
connection:Disconnect()
module.onDeath(player)
end
end)
if player:FindFirstChild("damage") == nil then
newFolder = Instance.new("Folder")
newFolder.Name = "damage"
newFolder.Parent = player
end
if newFolder:FindFirstChild(tostring(attacker)) == nil then
record = Instance.new("IntValue")
record.Name = tostring(attacker)
record.Parent = newFolder
end
if record.Value >= 100 then -- sanity check
else
record.Value = record.Value + damage
end
return humanoid:TakeDamage(damage)
end
Which triggers this code:
function module.onDeath(player)
if player:FindFirstChild("damage") ~= nil then
local folder = player:WaitForChild("damage")
local contents = folder:GetChildren()
for i,v in pairs(contents) do
local item = folder[tostring(v)]
damages[item.Name] = item.Value
end
end
local debounce = false
if not debounce then
debounce = true
module.handleKill(damages)
end
end
This is the sword script's function:
function Blow(Hit)
if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
return
end
local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
if not RightArm then
return
end
local RightGrip = RightArm:FindFirstChild("RightGrip")
if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
return
end
local character = Hit.Parent
if character == Character then
return
end
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid or humanoid.Health == 0 then
return
end
local player = Players:GetPlayerFromCharacter(character)
if player and player == Player then
return
end
UntagHumanoid(humanoid)
TagHumanoid(humanoid, Player)
module.damageHumanoid(plur, player, humanoid, Damage)
end
So I guess the way I was trying to use debounce isn't correct anymore. I referred to the Roblox documentation and found this: https://create.roblox.com/docs/scripting/scripts/debounce
If anyone would like to know what ended up working for me here it is:
humanoid.Died:Connect(function()
if not humanoid:GetAttribute("Killed") then
humanoid:SetAttribute("Killed", true)
module.onDeath(player)
task.wait(10)
humanoid:SetAttribute("Killed", true)
end
end)

Character doesn't immediatley go to the correct height when changing hipheight

For some reason, when I change a characters hipheight in roblox studio, they are a bit taller than they should be for a bit, then settle down to normal. Here is the code for my hipheight changing script:
--// Configuration
local keybind = "C"
local animationId = 11144547033 --> Replace the number with your animation ID!
--// Variables
local UserInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local hip = humanoid.HipHeight
local animation = Instance.new("Animation")
animation.AnimationId = 'rbxassetid://' .. animationId
local animationTrack = humanoid:WaitForChild("Animator"):LoadAnimation(animation)
animationTrack.Priority = Enum.AnimationPriority.Action
local crouching = false
--// Functions
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode[keybind] then
if crouching then
humanoid.WalkSpeed = 16
crouching = false
animationTrack:Stop()
character.HumanoidRootPart.CanCollide = true
humanoid.HipHeight = hip
else
humanoid.WalkSpeed = 7
humanoid.HipHeight = 1.5
crouching = true
animationTrack:Play()
end
end
end)
, and what happens
YT Vid

Descendantadded not firing?

im trying to make it so that the enemy moves to the sound value parent's position but descendantadded is not firing, i added a little print thingy on the if statement for soundvalue but it doesnt print.
local debounce = false
local prevpath = nil
workspace.DescendantAdded:Connect(function(sound)
--for i,sound in pairs(workspace:GetDescendants()) do
if sound.Name == "Soundvalue" and sound:IsA("StringValue") then
print("Founded")
local hrp = script.Parent:WaitForChild("Torso")
local ischasing = script.Parent:WaitForChild("Ischasing")
if ischasing.Value == false then
if not debounce then
debounce = true
if prevpath ~= nil then
prevpath:Destroy()
end
if sound.Parent and sound.Parent:IsA("BasePart") then
print("aaaaa")
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(hrp.Position, sound.Parent.Position)
local waypoints = path:GetWaypoints()
prevpath = path
for i,waypoint in pairs(waypoints) do
if ischasing.Value == false then
script.Parent.Enemy:MoveTo(waypoint.Position)
script.Parent.Enemy.MoveToFinished:Wait()
end
end
end
debounce = false
end
end
end
--end
end)

(Also roblox related)Countdown freezes at a random number

so there are 2 scripts: script and localscript.
The script determines how many seconds there will be and then it fires it to all player clients & the localscript changes the text to seconds. But when i tested the game the countdow freezes either at a number or a random number anyway here's the script again:
script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("IntermissonEvent")
local secondsRemaining = 15
for t = secondsRemaining, 0, -1 do
remoteEvent:FireAllClients(t)
wait(1)
end
localscript:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("IntermissonEvent")
local function onTimerUpdate(seconds)
script.Parent.Text =(seconds)
wait(15)
script.Parent.Parent.CountdownText.Visible = false
script.Parent.Parent.IntermissionText.Visible = false
script.Parent.Parent.TextLabel.Text ="Vote for a Map!"
script.Parent.Parent.MapVotingFrame.Visible = true
end
remoteEvent.OnClientEvent:Connect(onTimerUpdate)
Your LocalScript is running all of this logic every single time the server fires the event. Instead of waiting, check the value with an if-then.
local function onTimerUpdate(seconds)
script.Parent.Text = tostring(seconds)
if seconds == 0 then
script.Parent.Parent.CountdownText.Visible = false
script.Parent.Parent.IntermissionText.Visible = false
script.Parent.Parent.TextLabel.Text = "Vote for a Map!"
script.Parent.Parent.MapVotingFrame.Visible = true
end
end

How do I change a value in the leaderstats via touching a part? (Roblox)

Here's the error and what I've tried.
Error: Workspace.Part.Script:4: attempt to index nil with 'leaderstats'
My code:
Making Leaderstats:
game.Players.PlayerAdded:Connect(function(plr)
local ls = Instance.new("Model")
ls.Name = "leaderstats"
ls.Parent = plr
local m = Instance.new("IntValue")
m.Name = "Stars"
m.Value = 0
m.Parent = ls
end)
Touch Code:
local collected = false
script.Parent.Touched:Connect(function()
if collected == false then
game.Players.LocalPlayer.leaderstats.Stars.Value = game.Players.LocalPlayer.leaderstats.Stars.Value + 1
end
collected = true
end)
A server script would suit your need better. In a localscript, the change would only appear for the player. Also, it's better practice to use the Players service offered by Roblox. Here's an example:
local Players = game:GetService('Players')
local collected = false
script.Parent.Touched:Connect(function(partTouched)
if partTouched.Parent:IsA('Model') and partTouched.Parent:FindFirstChild('Humanoid') and partTouched:IsA('BasePart') then -- Is this a player? If so, then..
local player = Players:GetPlayerFromCharacter(partTouched.Parent)
if collected == false then
player.leaderstats.Stars.Value += 1 -- shorter syntax
end
collected = true
--script.Parent:Destroy() optional if this part won't be used again.
end
end)
If you're planning to use this for many parts, using a central ModuleScript would save you lots of changing things back and forth: https://developer.roblox.com/en-us/api-reference/class/ModuleScript