I am trying to make a swimmable part object, and the script I tried to piece together isn’t working. I want to change the HumanoidStateType of the player to swimming when the water part is touched.
Here is the script:
local water = script.Parent
water.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
while wait() do
game.Players.LocalPlayer.Character.Humanoid:ChangeState(Enum.HumanoidStateType.Swimming)
end
end
end)
Could someone please explain how I could change the script to get it working?
2 Likes
I believe this is a regular script inside the part. LocalPlayer doesn’t exist on the server so you can only do LocalPlayer on a local script. You can get character by using :GetPlayerFromCharacter() when you check an actual character touched to it.
I’m assuming that this is in a normal script? You can only use game.Players.LocalPlayer in local scripts, so you want to change that. I’ve never done this thing before so I don’t know if this would work but try this:
local water = script.Parent
local debounce = false --add a debounce so that the player doesn't hit the part 50 times in a second
water.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if debounce == false then
debounce = true
hit.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Swimming)
wait(0.5)
debounce = false
end
end
end)
water.TouchEnded:Connect(function(hit) --to make them stop swimming
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Walking)
end
end)
instead of debouncing the water itself, create a table of all swimming characters so you only index a character once while also causing no delay between other swimmers who may jump in the same body of water within the half-second delay
local water = script.Parent
local taggedChars = {}
water.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and not taggedChars[hit.Parent] then
taggedChars[hit.Parent] = true -- applies the tag
hit.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Swimming)
end
end)
water.TouchEnded:Connect(function(hit) -- to make them stop swimming
if taggedChars[hit.Parent] then
taggedChars[hit.Parent] = nil -- removes the key, player is not tagged anymore
hit.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
end
end)
Next time, please mark a solution instead of withdrawing the post, thanks!
Sorry, I tried your script, there were no errors but it didn’t work. Then, I tried @XdJackyboiiXd21’s script and it said there was no walking type, so I looked it up and changed it to Running. It still didn’t work for some reason. I am using a regular script. I can send you a screenshot if you want later, but I’m going somewhere right now.
how does it not work? what exactly is the issue? could you provide us with a gif or screenshot?
Here is a screenshot:
When I run it and I walk into the water part, it acts like normal.
send me a screenshot of my script applied
Hello, I have tried another script and in this one at least the swimming seems to be working, but it has other issues. I have covered it in this short video.
1 Like